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

Recursive types: Bug for struct with multiple recursive fields or oneOf #1297

Closed
allengu01 opened this issue Aug 12, 2024 · 1 comment
Closed
Labels
bug Something isn't working

Comments

@allengu01
Copy link

allengu01 commented Aug 12, 2024

What version of ogen are you using?

v1.2.2

Can this issue be reproduced with the latest version?

Yes

What did you do?

For the schema below, generating gives an error. However, removing either the b or c field under the schema for A works.

invalid recursive type A
oas_schemas_gen.go(6, 6): A refers to
oas_schemas_gen.go(162, 6): OptC refers to
oas_schemas_gen.go(47, 6): C refers to
oas_schemas_gen.go(70, 6): OptA refers to
oas_schemas_gen.go(6, 6): A
openapi: 3.0.2
servers:
  - url: /api/v1
info:
  version: 1.0.0
  title: Test API
paths:
  /test:
    post:
      operationId: test
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/A'
      responses:
        '200':
          content:
            application/json:
              schema:
                type: string

components:    
  schemas:
    A:
      type: object
      properties:
        b:
          $ref: '#/components/schemas/B'
        c:
          $ref: '#/components/schemas/C'
    B:
      type: object
      properties:
        a:
          $ref: '#/components/schemas/A'
    C:
      type: object
      properties:
        a:
          $ref: '#/components/schemas/A'

Having a recursive field within oneOf also errors:

openapi: 3.0.2
servers:
  - url: /api/v1
info:
  version: 1.0.0
  title: Test API
paths:
  /test:
    post:
      operationId: test
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/A'
      responses:
        '200':
          content:
            application/json:
              schema:
                type: string

components:    
  schemas:
    A:
      type: object
      properties:
        b:
          $ref: '#/components/schemas/B'
    B:
      type: object
      properties:
        a:
          oneOf:
            - $ref: '#/components/schemas/A'
            - type: boolean

What did you expect to see?

From the docs, recursive types should be using pointers to avoid errors with Golang structs.

What did you see instead?

A's type looks like:

type A struct {
	B OptB `json:"b"`
	C OptC `json:"c"`
}

B's type looks like:

type B struct {
	A *A `json:"a"`
}

C's type, however, looks like:

type C struct {
	A OptA `json:"a"`
}
@allengu01 allengu01 added the bug Something isn't working label Aug 12, 2024
@allengu01 allengu01 changed the title Recursive types: Bug for struct with multiple recursive fields Recursive types: Bug for struct with multiple recursive fields or oneOf Aug 12, 2024
@allengu01
Copy link
Author

fixed in v1.3.0

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