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

[gen] faulty code generation for sum types with reusable fields #2847

Closed
hochgi opened this issue May 16, 2024 · 0 comments · Fixed by #2850
Closed

[gen] faulty code generation for sum types with reusable fields #2847

hochgi opened this issue May 16, 2024 · 0 comments · Fixed by #2850
Labels
bug Something isn't working

Comments

@hochgi
Copy link
Contributor

hochgi commented May 16, 2024

Describe the bug
When defining in open API a component that only reference "subtypes", which all include a shared other component,
the generated code will:

  1. not compile
  2. has duplicate classes (subtype are both nested in sealed trait companion, and have standalone redundant copy)
  3. shared fields is not referenced by any endpoint, but has its own redundant class generated

To Reproduce
generate from the following example:

info:
  title: Animals Service
  version: 0.0.1
servers:
  - url: http://127.0.0.1:5000/
tags:
  - name: Animals_API
paths:
  /api/v1/zoo/{animal}:
    get:
      operationId: get_animal
      parameters:
        - in: path
          name: animal
          schema:
            type: string
          required: true
      tags:
        - Animals_API
      description: Get animals by species name
      responses:
        "200":
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Animal'
          description: OK
        "500":
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HttpError'
          description: Internal Server Error
openapi: 3.0.3
components:
  schemas:
    Animal:
      oneOf:
        - $ref: '#/components/schemas/Alligator'
        - $ref: '#/components/schemas/Zebra'
    AnimalSharedFields:
      type: object
      required:
        - age
        - weight
      properties:
        age:
          type: integer
          format: int32
          minimum: 0
        weight:
          type: number
          format: float
          minimum: 0
    Alligator:
      allOf:
        - $ref: '#/components/schemas/AnimalSharedFields'
        - type: object
          required:
            - num_teeth
          properties:
            num_teeth:
              type: integer
              format: int32
              minimum: 0
    Zebra:
      allOf:
        - $ref: '#/components/schemas/AnimalSharedFields'
        - type: object
          required:
            - num_stripes
          properties:
            num_stripes:
              type: integer
              format: int32
              minimum: 0
    HttpError:
      type: object
      properties:
        messages:
          type: string

and examine the generated classes:

tree target/scala-3.3.3/src_managed
target/scala-3.3.3/src_managed
└── zoo
    └── openapi
        ├── api
        │   └── v1
        │       └── zoo
        │           └── Animal.scala
        └── component
            ├── Alligator.scala
            ├── Animal.scala
            ├── AnimalSharedFields.scala
            ├── HttpError.scala
            └── Zebra.scala

7 directories, 6 files

Expected behaviour

  1. Code needs to compile
  2. No duplicate classes
  3. No generation of classes not referenced from an endpoint (e.g. shared fields for reuse in other classes)
  4. (*) bonus: if all subtypes of a sealed trait include the same shared fields component, add the fields as members of the trait, e.g. in this case:
@noDiscriminator
sealed trait Animal {
  def age: Int
  def weight: Float
}

Screenshots
compile_error
all_generated_classes

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

Successfully merging a pull request may close this issue.

1 participant