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

Group schema components by content-type #3168

Open
souenzzo opened this issue Feb 21, 2023 · 10 comments
Open

Group schema components by content-type #3168

souenzzo opened this issue Feb 21, 2023 · 10 comments
Labels
re-use: ref-group-combine Re-use requests involving grouping components and combining groups

Comments

@souenzzo
Copy link

I'm working on an API that has many content-type, and they use references to schema

The references to the user entity, for example, aren't the same on each content-type.

I'm currently handling that by adding a content-type prefix on each schema:

schemas:
  application/json_user:
    type: object
  application/custom+json_user:
    type: object

I'd like to use something like

schemas:
  application/json:
    user:
      ...
  application/custom+json:
    user:
      ...
@MikeRalphson
Copy link
Member

Is your desire to group the schemas for ease of editing, production of documentation or another use-case?

@souenzzo
Copy link
Author

Because they are distinct and I don't want to pollute the name of the schema

For example, in my application/custom+json content-type, we wrap enums with objects like {_t: enum, _v: "admin"}

So we have 2 distinct user schemas, one for application/json, other for application/custom+json

schemas:
  application/json_user:
    type: object
    properties:
      id:
        type: string
      role:
        type: string
        enum: [ admin, regular ]
  application/custom+json_user:
    type: object
    properties:
      id:
        type: string
      role:
        type: object
        properties:
          _t:
            type: string
            enum: [ enum ]
          _v:
            type: string
            enum: [ admin, regular ]

@karenetheridge
Copy link
Member

You could use subschemas inside /components/schemas, via $defs:

components:
  schemas:
    user:
      $defs:
        application/json:
          ..
        application/custom:
          ..

And then reference them as "$ref": "#/components/schemas/user/$defs/application/json"

@souenzzo
Copy link
Author

@karenetheridge

This indeed partially works, but swagger-ui does not support this kind of references

data:application/json;base64,eyJjb21wb25lbnRzIjp7InNjaGVtYXMiOnsidXNlciI6eyIkZGVmcyI6eyJhIjp7InR5cGUiOiJudW1iZXIifX19fX0sIm9wZW5hcGkiOiIzLjAuMyIsInBhdGhzIjp7Ii8iOnsiZ2V0Ijp7InJlc3BvbnNlcyI6eyJkZWZhdWx0Ijp7ImNvbnRlbnQiOnsiYXBwbGljYXRpb24vanNvbiI6eyJzY2hlbWEiOnsiJHJlZiI6IiMvY29tcG9uZW50cy9zY2hlbWFzL3VzZXIvJGRlZnMvYSJ9fX19fX19fX0=

Screenshot 2023-02-22 at 07 09 50

As we can see in the image

  • The resolution of the schema works (we can find the number type in GET / route
  • The bottom Schemas component does not support this kind of reference, showing an empty group

I would like to have an explicit example of this in OAI spec, then swagger-ui and other tools will be able to implement proper support.

@MikeRalphson
Copy link
Member

I would like to have an explicit example of this in OAI spec, then swagger-ui and other tools will be able to implement proper support.

@karenetheridge it would be great if you could PR a new example to the examples/v3.1 directory 😁

@souenzzo
Copy link
Author

@MikeRalphson I can do this PR?

@handrews
Copy link
Member

handrews commented Mar 1, 2023

Note that $defs is only allowed in OAS 3.1 and later, which Swagger UI does not support.

@souenzzo
Copy link
Author

souenzzo commented Mar 13, 2023

Something like this

components:
  schemas:
    Error:
      $defs:
        application/json:
          type: object
            required:
              - code
              - message
            properties:
              code:
                type: integer
                format: int32
              message:
                type: string
        text/plain:
          type: string

Can be added to petstore example

@souenzzo
Copy link
Author

@MikeRalphson // @karenetheridge

is there anything i can do to help?

@handrews
Copy link
Member

@souenzzo anyone can submit a PR! For a PR on the examples, you need a slight change as the first thing under /components/schemas still needs to be a schema name:

components:
  schemas:
    Error:
      $defs:
        application/json:
          type: object
          required:
            - code
            - message
          properties:
            code: 
              type: integer
              format: int32 
            message:
              type: string
        text/plain:
          type: string

So you would use "$ref": "#/components/schemas/Error/$defs/application~1json" to reference the JSON version, and "$ref": "#/components/schemas/Error/$defs/text~1plain" to references the plain text version. (The ~1 is how JSON Pointer escapes the presence of a /).

It's probably worth making a separate example rather than changing Petstore as my impression is that Petstore is the basic starting point. But that's just my impression, not any sort of official guidance.

Beyond that, you'll need swagger-ui to support OAS 3.1 which looks like it might happen in version 5.0. Theres nothing that the OAS spec can do about that.

@handrews handrews added $ref re-use: ref-group-combine Re-use requests involving grouping components and combining groups and removed $ref labels Jan 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
re-use: ref-group-combine Re-use requests involving grouping components and combining groups
Projects
None yet
Development

No branches or pull requests

4 participants