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

Parsing OpenAPI 2 specs with discriminator fails

Open patrick246 opened this issue 3 years ago • 2 comments

When loading an OpenAPI 2 spec that is using discriminator in models, then the spec parsing fails with failed to unmarshal property "definitions" (*map[string]*openapi3.SchemaRef): failed to unmarshal property "discriminator" (*openapi3.Discriminator): failed to unmarshal extension properties: json: cannot unmarshal string into Go value of type map[string]json.RawMessage ("type")

Example: https://gist.github.com/patrick246/e8b329144d228b4a1892946162868e50

This issue was encountered by someone before (#214), but worked around by mixing OpenAPI 3 parts into their OpenAPI 2 spec. As far as I can tell, this is because the library is using OpenAPI 3 data types, but the discriminator changed between 2 and 3: OpenAPI 2 uses string, while OpenAPI 3 uses a discriminator object.

patrick246 avatar May 10 '21 14:05 patrick246

Would be great to resolve this ;)

roberth1988 avatar Jul 18 '21 14:07 roberth1988

I assume nobody is too focused on 2.0 at this point but, just in case... It is also an issue if you try to generate a new 2.0 spec with a model that has a discriminator. You won't be able to because you will have to define the Discriminator property as a *openapi3.Discriminator.

I did find a simple workaround in this case though, use extensions:

			Extensions: map[string]interface{}{
				"discriminator": "petType",
			},

crochik avatar May 17 '23 21:05 crochik