kin-openapi
kin-openapi copied to clipboard
`nullable` not respected on `$ref` schemas
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!
}
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.
openapi3.0 does not support the full JSON Schema spec. It is in openapi3.1 and this is still an open issue
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;
};