OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.82k stars 6.58k forks source link

[BUG] Missing model files when using embedded/splitted spec files #4710

Open zolv opened 4 years ago

zolv commented 4 years ago
Description

If I split YML file into 2 files, unused models (not used as a request or response body) are not generated. If everything is in a single file it works just fine. So for an example below, model files created: Parent.java. Model files NOT created: ChildOne.java and ChildTwo.java. If we add e.g. ChildTwo entity into a post response, then it is generated, but ChildOne.java isn't.

openapi-generator version

4.2.2

OpenAPI declaration file content or url

Test project which reveals this issue: https://github.com/zolv/openapi-generation-bug-report-1

Spec files (splitted):

api-split.yml

openapi: 3.0.2
info:
  title: API Documentation
  version: 1.0.0
servers:
  - url: http://localhost:8080/
paths:
  /users:
    $ref: './api-split-sub.yml#/test'

api-split-sub.yml

test:
   post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Parent'
      responses:
        200:
          description: It's OK

components:
  schemas:

    Parent:
      required:
        - status
      properties:
        status:
          type: string
      discriminator:
        propertyName: status
        mapping:
          CHILD_ONE: '#/components/schemas/ChildOne'
          CHILD_TWO: '#/components/schemas/ChildTwo'

    ChildOne:
      properties:
        status:
          type: string
      discriminator:
        - $ref: '#/components/schemas/Parent'
        - type: object

    ChildTwo:
      allOf:
        - $ref: '#/components/schemas/Parent'
        - type: object
Command line used for generation

./gradlew clean openApiGenerate -s

Steps to reproduce

Nothing. It's just a command line execution.

Related issues/PRs

Nothing I could find. My issue was first asked on SO

Suggest a fix

No suggestion for fix. WOrkaround: put everything into single file.

cvgaviao commented 4 years ago

OpenAPI 3.0.2 do not allows you to reference a path the way you are doing. You are allowed to reference parameters, headers, responses, schemas, but not path !

  /{retailer-id}/agents:
    parameters:
      - $ref: 'internal.common.oas3.yaml#/components/parameters/ParamP_RetailerId'
    get:
      summary: Retrieves a collection of delivery agents.
      operationId: getDeliveryAgents
      responses:
        '200':
          $ref: "#/components/responses/200_DELIVERY_AGENTS_READ"
zolv commented 4 years ago

Then why isn't it failing on validation? I've checked also using Spectral validator and it is also fine.

I've tried Your suggestion:

api.yml:

openapi: 3.0.2
info:
  title: API Documentation
  version: 1.0.0
servers:
  - url: http://localhost:8080/
paths:
  /users:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: 'api-sub.yml#/components/schemas/Parent'
      responses:
        200:
          description: New user created

api-sub.yml:

components:
  schemas:

    Parent:
      required:
        - status
      properties:
        status:
          type: string
      discriminator:
        propertyName: status
        mapping:
          CHILD_ONE: '#/components/schemas/ChildOne'
          CHILD_TWO: '#/components/schemas/ChildTwo'

    ChildOne:
      properties:
        status:
          type: string
      discriminator:
        - $ref: '#/components/schemas/Parent'
        - type: object

    ChildTwo:
      allOf:
        - $ref: '#/components/schemas/Parent'
        - type: object

But still the same - only Parent.java is generated.

cvgaviao commented 4 years ago

Then why isn't it failing on validation?

Yep, I'm wrong... sorry. It was the tool and parser that I was using before that did not supported $ref in paths. OpenAPI spec supports it.

My referenced schemas in another file are being generated without problems, but none of them is being referenced in a mapping statement. Maybe this is not supported yet.

Try to reference ChildOne or ChildTwo in some Response...

thiko commented 4 years ago

Hi,

unfortunately, using the models in a Response does not solve this issue. The model files are not generated after splitting the yaml part in multiple files.