Skip to content

gen: Nested sum types generate broken (non-compilable) code #1480

@abemedia

Description

@abemedia

What version of ogen are you using?

v1.14.0

Can this issue be reproduced with the latest version?

Yes

What did you do?

I ran ogen against a spec with nested sum types. See the following spec:

openapi: 3.0.0
info:
  title: title
  version: v0.1.0
paths:
  /test:
    get:
      operationId: test
      responses:
        '200':
          description: Test
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Sum'
components:
  schemas:
    Sum:
      oneOf:
        - $ref: '#/components/schemas/Sum1'
        - $ref: '#/components/schemas/Sum2'
    Sum1:
      oneOf:
        - type: object
          required:
            - common-1
            - unique-1
          properties:
            common-1:
              type: string
            unique-1:
              type: string
        - type: object
          required:
            - common-1
            - unique-2
          properties:
            common-1:
              type: string
            unique-2:
              type: string
    Sum2:
      oneOf:
        - type: object
          required:
            - common-1
            - otherUnique-1
          properties:
            common-1:
              type: string
            otherUnique-1:
              type: string
        - type: object
          required:
            - common-1
            - otherUnique-2
          properties:
            common-1:
              type: string
            otherUnique-2:
              type: string

What did you expect to see?

Ogen generates code to determine what the type is based on unique fields.

What did you see instead?

Broken code is generated with a switch containing duplicate cases of jx.Object.

func (s *Sum) Decode(d *jx.Decoder) error {
	if s == nil {
		return errors.New("invalid: unable to decode Sum to nil")
	}
	// Sum type type_discriminator.
	switch t := d.Next(); t {
	case jx.Object:
		if err := s.Sum1.Decode(d); err != nil {
			return err
		}
		s.Type = Sum1Sum
	case jx.Object:
		if err := s.Sum2.Decode(d); err != nil {
			return err
		}
		s.Type = Sum2Sum
	default:
		return errors.Errorf("unexpected json type %q", t)
	}
	return nil
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions