kin-openapi icon indicating copy to clipboard operation
kin-openapi copied to clipboard

`nullable` not respected on `$ref` schemas

Open brandonbloom opened this issue 1 year ago • 3 comments

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 avatar Mar 29 '24 23:03 brandonbloom

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.

brandonbloom avatar Mar 30 '24 03:03 brandonbloom

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

fenollp avatar Mar 30 '24 09:03 fenollp

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;
};

brandonbloom avatar Mar 30 '24 16:03 brandonbloom