APIDevTools / swagger-cli

Swagger 2.0 and OpenAPI 3.0 command-line tool
https://apitools.dev/swagger-cli
MIT License
517 stars 68 forks source link

Combining multiple files into one keeps references to some api files #87

Open apoza opened 1 year ago

apoza commented 1 year ago

Problem

I have an API splitted in several files and I have used swagger-cli to combine those files into one (to use that file in API consumer services/applications) When any API file is referenced using $ref that file is exported (included) in the new API file as component (that's good). But those files that are referenced without using $ref are exported (included in the bundled file) keeping the reference to the file.

Example

Let's suppose that we have OpenApi.yaml that has this endpoint deffinition

 [...]
 /offering:
    post:
      [...]
      requestBody:
        description: This is an offering
        required: true
        content:
          application/json:
            schema:
              $ref: ./Offering.yaml
 [...]

This is Offering.yaml

title: Offering
description: Interface of offering requests
oneOf:
  - $ref: ./LocalOffering.yaml
  - $ref: ./InternationalOffering.yaml
discriminator:
  propertyName: type
  mapping:
    local: ./LocalOffering.yaml
    international: ./InternationalOffering.yaml

When I combine the API files (executing: swagger-cli bundle OpenApi.yaml --outfile CombinedApi.yaml --type yaml) this is the outcome (CombinedApi.yaml):

 [...]
Offering:
      title: Offering
      description: Interface of offering requests
      oneOf:
        - $ref: '#/components/schemas/LocalOffering'
        - $ref: '#/components/schemas/InternationalOffering'
      discriminator:
        propertyName: type
        mapping:
          local: ./LocalOffering.yaml
          international: ./InternationalOffering.yaml
 [...]

As we can see the discriminator.mapping.local and discriminator.mapping.international keeps files references. That means that we need those files so the combination in one file is not valid

Expected behavior

I would expect than those files referenced without using $ref would be exported in a way (maybe the same way that $ref ones) that doesn't keep the reference to the files.

Expected outcome:

 [...]
Offering:
      title: Offering
      description: Interface of offering requests
      oneOf:
        - $ref: '#/components/schemas/LocalOffering'
        - $ref: '#/components/schemas/InternationalOffering'
      discriminator:
        propertyName: type
        mapping:
          local: '#/components/schemas/LocalOffering'
          international: '#/components/schemas/InternationalOffering'
 [...]
MateusFrFreitas commented 1 year ago

+1