Git Product home page Git Product logo

Comments (13)

aaldredge avatar aaldredge commented on June 11, 2024 12

I would also like to have this feature. Specifically for OpenAPI 3, It would be great to have an option where bundle takes the referenced components and combines them in a components section in a bundled file, updating the original $ref that pointed to <file>#<componentPath> to point to #<componentPath> in the combined file.

from swagger-cli.

pauldraper avatar pauldraper commented on June 11, 2024 5

Copying from APIDevTools/swagger-parser#127 (comment)


The obvious -- if annoying -- workaround is to put components first, and reference everything there.

Example input

example.yml

openapi: "3.0.3"
components: # reference everything here
  schemas:
    a: { $ref: "./schema/a.yml" }
    b: { $ref: "./schema/b.yml" }
info:
  title: Example
  version: 0.0.0
paths:
  /a:
    get:
      responses:
        "200":
           content:
             application/json:
               schema: { $ref: "./schema/a.yml" }
           description: Success
  /b:
    get:
      responses:
        "200":
          content:
            application/json:
              schema: { $ref: "./schema/b.yml" }
          description: Success

schema/a.yml

properties:
  aName: { type: string }
  b: { $ref: "./b.yml" }

schema/b.yml

properties:
  bName: { type: string }
Example output
swagger-cli bundle example.yml

yields

openapi: 3.0.3
components:
  schemas:
    a:
      properties:
        aName:
          type: string
        b:
          $ref: '#/components/schemas/b'
    b:
      properties:
        bName:
          type: string
info:
  title: Example
  version: 0.0.0
paths:
  /a:
    get:
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/a'
          description: Success
  /b:
    get:
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/b'
          description: Success

from swagger-cli.

MaxDesplanches avatar MaxDesplanches commented on June 11, 2024 4

Hello there,

I am waiting to have ref internal or to ignore hasardous generation:
#/paths/~1users~1address~1%7Bid%7D/get/responses/200/content/application~1json/schema/properties/data

During this time, I am coding some split string things to get what I want in my original file to get internal components to bundle a valid file.

Thank in advance

from swagger-cli.

stephanebachelier avatar stephanebachelier commented on June 11, 2024 2

Thanks @lorthirk. Using --dereference fix my issues with circular ref. Having this feature would be really awesome !

from swagger-cli.

AlbinoDrought avatar AlbinoDrought commented on June 11, 2024 2

In our projects we only have a few shared schemas. A workaround we've been using because of this problem is to avoid code like this:

swagger: '2.0'
info:
  version: 0.0.0
paths:
  /test1:
    get:
      parameters:
        - $ref: "./parameters.yaml#/parameters/param"
  /test2:
    get:
      parameters:
        - $ref: "./parameters.yaml#/parameters/param"

and use code like this instead:

swagger: '2.0'
info:
  version: 0.0.0
paths:
  /test1:
    get:
      parameters:
        - $ref: "#/parameters/param"
  /test2:
    get:
      parameters:
        - $ref: "#/parameters/param"
parameters:
  param:
    $ref: "./parameters.yaml#/parameters/param"

This gets bundled into:

swagger: '2.0'
info:
  version: 0.0.0
paths:
  /test1:
    get:
      parameters:
        - $ref: "#/parameters/param"
  /test2:
    get:
      parameters:
        - $ref: "#/parameters/param"

parameters:
  param:
    in: query
    name: myParam
    description: some demo parameter
    type: string
    pattern: '^[a-zA-Z]+$'
    required: true

which works with our code generators but takes some extra lines.

from swagger-cli.

lorthirk avatar lorthirk commented on June 11, 2024 1

A +1 on this, since I have a circular reference on my project and the code generation breaks if I don't use --dereference, which I have to because of the circular ref.

from swagger-cli.

aaldredge avatar aaldredge commented on June 11, 2024 1

@stephanebachelier thanks for the tag.

I tried with the latest version (2.3.4) using the command
swagger-cli bundle schemas/specs/all.yaml -t yaml -o schemas/resolved/all.yaml and I am still seeing the reference inlined and then referenced later with a $ref to the inlined occurrence. This results in refs such as:

- $ref: '#/paths/~1v1~1my~1path/get/parameters/0'

and this beauty

- $ref: '#/paths/~1v1~1my~1other-path/get/responses/200/content/application~1json/schema/allOf/1/properties/data/items/oneOf/0/properties/dataPoint/allOf/0'

from swagger-cli.

mgrzechocinski avatar mgrzechocinski commented on June 11, 2024

Would love to have this feature too!

from swagger-cli.

stephanebachelier avatar stephanebachelier commented on June 11, 2024

@lorthirk @aaldredge @mgrzechocinski @joffrey-bion it seems that it already exists as long as you manage to avoid circular references. I add forgotten to add some schemas before the paths sections. Adding them before the path sections remove the circular references are they are already defined in the bundled schema.

Now mystery of references are rewritten using schema references :
From $ref: ../../components/responses/index.yaml#/Unauthenticated, the reference is changed to $ref: '#/components/responses/Unauthenticated' in the bundled file.

And the Unauthenticated is existing only in the #/components/responses section.

from swagger-cli.

Bogdaan avatar Bogdaan commented on June 11, 2024

+1, very important feature. With --dereference we unable to generate valid client specification.

There is no semantic names for schemas:

  /random:
    get:
      summary: todo.
      description: todo.
      security:
        - bearerAuth: []
      responses:
        '200':
          description: Successful result.
          content:
            application/json:
              schema:
                type: object
                required:
                  - categories
                properties:
                  categories:
                    type: object
                    required:
                      - name
                    properties:
                      name:
                        type: string

from swagger-cli.

theaxel avatar theaxel commented on June 11, 2024

Would be great to have a solution for this!
Running into the same issue.

from swagger-cli.

bu4ak avatar bu4ak commented on June 11, 2024

+

from swagger-cli.

aleskovets avatar aleskovets commented on June 11, 2024

Looks related to APIDevTools/swagger-parser#127

from swagger-cli.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.