swaggest / openapi-go

OpenAPI structures for Go
https://pkg.go.dev/github.com/swaggest/openapi-go/openapi3
MIT License
221 stars 18 forks source link

Unmarshal failed to openAI spec #114

Closed clockfly closed 1 day ago

clockfly commented 4 weeks ago

Describe the bug Unmarshal yaml failed for https://github.com/openai/openai-openapi/blob/master/openapi.yaml

Error:

oneOf constraint failed for SchemaOrRef with 0 valid results: map[Schema:oneOf constraint failed for SchemaOrRef with 0 valid results: map[Schema:additional properties not allowed in Schema: [$ref] SchemaReference:additional properties not allowed in SchemaReference: [nullable]] SchemaReference:required key missing: $ref]

To Reproduce

yamlFile = "path_to_openapi.yaml"
bytes, _ := ioutil.ReadFile(yamlFile)
spec.UnmarshalYAML(bytes); err != nil {
    fmt.Println(err)
}

Expected behavior This file should be parsed correctly. This file renders correctly in swagger editor.

https://editor-next.swagger.io/

image

Additional context None

vearutop commented 3 weeks ago

The spec in question is not a valid one.

If you open it in a schema-enabled editor, like the one included in intellij IDEs (e.g. GoLand), you'll see warnings. image

The issue here is that OpenAPI 3.0 is based on JSON Schema draft 04, which does not allow any fields adjacent to $ref. This is also what is told by unmarshal error message (in a cryptic way though):

additional properties not allowed in SchemaReference: [nullable]]

Here are original and fixed schemas: openai-openapi.json Converted from the YAML original. openai-openapi-fixed.json Fixed to pass unmarshaling.