Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nullable not respected on $ref schemas #927

Open
brandonbloom opened this issue Mar 29, 2024 · 3 comments · May be fixed by #928
Open

nullable not respected on $ref schemas #927

brandonbloom opened this issue Mar 29, 2024 · 3 comments · May be fixed by #928

Comments

@brandonbloom
Copy link
Contributor

package openapi3

import (
	"testing"

	"github.com/stretchr/testify/require"
)

func TestIssueXXX(t *testing.T) {
	spec := `
openapi: 3.0.0
components:
  schemas:
    NullableString:
      type: string
      nullable: true
    NullableRef:
      $ref: "#/components/schemas/String"
      nullable: true
    String:
      type: string
`

	sl := NewLoader()
	doc, err := sl.LoadFromData([]byte(spec))
	require.NoError(t, err)

	require.False(t, doc.Components.Schemas["String"].Value.Nullable)
	require.True(t, doc.Components.Schemas["NullableString"].Value.Nullable)
	require.True(t, doc.Components.Schemas["NullableRef"].Value.Nullable) // this fails!
}
@brandonbloom
Copy link
Contributor Author

More generally, it seems like this part of the spec is not respected (emphasis mine):

8.2.3.1. Direct References with "$ref"

The "$ref" keyword is an applicator that is used to reference a statically identified schema. Its results are the results of the referenced schema. Note that this definition of how the results are determined means that other keywords can appear alongside of "$ref" in the same schema object.

@fenollp
Copy link
Collaborator

fenollp commented Mar 30, 2024

openapi3.0 does not support the full JSON Schema spec. It is in openapi3.1 and this is still an open issue

@brandonbloom
Copy link
Contributor Author

Apologies for the noise. I realized this after some more exploration/experimentation. I've got a workaround now:

export const refExtrasToAllOf = (schema: Schema): Schema => {
  if (RefSchema.guard(schema) && Object.keys(schema).length > 1) {
    const { $ref, ...extra } = schema;
    const allOf = AllOfSchema.guard(schema) ? schema.allOf : [];
    allOf.push({ $ref });
    return { ...extra, allOf };
  }
  return schema;
};

@fenollp fenollp linked a pull request Jul 3, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants