Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use allOf within an anyOf with discriminator #988

Closed
dan-j opened this issue Jul 27, 2023 · 2 comments
Closed

Unable to use allOf within an anyOf with discriminator #988

dan-j opened this issue Jul 27, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@dan-j
Copy link

dan-j commented Jul 27, 2023

What version of ogen are you using?

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

github.com/ogen-go/ogen v0.72.0

Can this issue be reproduced with the latest version?

Yes

What did you do?

Try to generate code from this schema:

openapi: 3.1.0
info:
  title: Test API
  version: 0.0.1
  license:
    name: MIT
    identifier: MIT
servers:
  - url: http://localhost:8080
paths:
  /vehicle:
    get:
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Vehicle'
        default:
          description: Error
          content:
            application/json:
              schema: {}
components:
  schemas:
    VehicleType:
      type: string
      enum:
        - car
        - bike
    VehicleBase:
      type: object
      properties:
        id:
          type: string
        type:
          $ref: '#/components/schemas/VehicleType'
        numberOfWheels:
          type: integer
          minimum: 0
    Car:
      type: object
      allOf:
        - $ref: '#/components/schemas/VehicleBase'
        - type: object
          properties:
            type:
              const: car
            numberOfDoors:
              type: integer
              minimum: 0
    Bicycle:
      type: object
      allOf:
        - $ref: '#/components/schemas/VehicleBase'
        - type: object
          properties:
            type:
              const: bike
            bicycleType:
              type: string
              enum:
                - road
                - mountain
                - hybrid
    Vehicle:
      type: object
      discriminator:
        propertyName: type
        mapping:
          car: '#/components/schemas/Car'
          bike: '#/components/schemas/Bicycle'
      oneOf:
        - $ref: '#/components/schemas/Car'
        - $ref: '#/components/schemas/Bicycle'

What did you expect to see?

The code generate successfully

What did you see instead?

The following error output:

generate:
    main.run
        PATH_TO_PROJECT/vendor/github.com/ogen-go/ogen/cmd/ogen/main.go:304
  - build IR:
    main.generate
        PATH_TO_PROJECT/vendor/github.com/ogen-go/ogen/cmd/ogen/main.go:64
  - make ir:
    github.com/ogen-go/ogen/gen.NewGenerator
        PATH_TO_PROJECT/vendor/github.com/ogen-go/ogen/gen/generator.go:112
  - operations:
    github.com/ogen-go/ogen/gen.(*Generator).makeIR
        PATH_TO_PROJECT/vendor/github.com/ogen-go/ogen/gen/generator.go:130
  - path "/vehicle": get:
    github.com/ogen-go/ogen/gen.(*Generator).makeOps
        PATH_TO_PROJECT/vendor/github.com/ogen-go/ogen/gen/generator.go:171
  - responses:
    github.com/ogen-go/ogen/gen.(*Generator).generateOperation
        PATH_TO_PROJECT/vendor/github.com/ogen-go/ogen/gen/gen_operation.go:55
  - code 200:
    github.com/ogen-go/ogen/gen.(*Generator).generateResponses
        PATH_TO_PROJECT/vendor/github.com/ogen-go/ogen/gen/gen_responses.go:34
  - contents:
    github.com/ogen-go/ogen/gen.(*Generator).responseToIR
        PATH_TO_PROJECT/vendor/github.com/ogen-go/ogen/gen/gen_responses.go:182
  - media: "application/json":
    github.com/ogen-go/ogen/gen.(*Generator).generateContents
        PATH_TO_PROJECT/vendor/github.com/ogen-go/ogen/gen/gen_contents.go:330
  - generate schema:
    github.com/ogen-go/ogen/gen.(*Generator).generateContents.func1
        PATH_TO_PROJECT/vendor/github.com/ogen-go/ogen/gen/gen_contents.go:271
  - oneOf:
    github.com/ogen-go/ogen/gen.(*schemaGen).generate2
        PATH_TO_PROJECT/vendor/github.com/ogen-go/ogen/gen/schema_gen.go:247
  - discriminator: unable to map "car" to "file://PATH_TO_PROJECT/openapi.yaml#/components/schemas/Car":
    github.com/ogen-go/ogen/gen.(*schemaGen).oneOf
        PATH_TO_PROJECT/vendor/github.com/ogen-go/ogen/gen/schema_gen_sum.go:297

It looks like because the Car/Bicycle schema is being merged from a allOf,

if vschema == v {
is false so it never finds a mapping.

@dan-j dan-j added the bug Something isn't working label Jul 27, 2023
@dan-j
Copy link
Author

dan-j commented Jul 27, 2023

Also, everything works if you omit the discriminator.mapping field, but many documentation tools use this to help the user browse the API so ideally we want to keep it in.

@dan-j
Copy link
Author

dan-j commented Aug 24, 2023

Solved by #979

@dan-j dan-j closed this as completed Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant