ogen icon indicating copy to clipboard operation
ogen copied to clipboard

ogen generates non-compilable code when discriminator mapping has duplicate values

Open johannes-riecken opened this issue 1 year ago • 0 comments

What version of ogen are you using?

$ go list -m github.com/ogen-go/ogen

ogen version v0.71.1 (built with go1.20.4) darwin/arm64

Can this issue be reproduced with the latest version?

Yes, that's the latest version.

What did you do?

I created an OpenAPI spec, which maps several values to the same schema, and ran ogen. Excerpt of full example:

    Shape:
      discriminator:
        mapping:
          Circle: '#/components/schemas/Circle'
          CircleSpecial: '#/components/schemas/Circle'
          Rectangle: '#/components/schemas/Rectangle'

What did you expect to see?

Ideally this should work, as it's not outlawed by the OpenAPI 3.1 spec (though I'll be curious how the JSON Schema standard will define it if it gets added). Or ogen should be more vocal about not being able to generate correct code.

What did you see instead?

ogen generates duplicate cases in oas_json_gen.go:

func (s Shape) Encode(e *jx.Encoder) {
	switch s.Type {
	case CircleShape:
		e.ObjStart()
		e.FieldStart("kind")
		e.Str("Circle")
		s.Circle.encodeFields(e)
		e.ObjEnd()
	case CircleShape:
		e.ObjStart()
		e.FieldStart("kind")
		e.Str("CircleSpecial")
		s.Circle.encodeFields(e)
		e.ObjEnd()

johannes-riecken avatar Jul 18 '23 12:07 johannes-riecken