fastify / fastify-swagger

Swagger documentation generator for Fastify
MIT License
910 stars 200 forks source link

Support for different content with the same http code. #745

Open Hydrock opened 1 year ago

Hydrock commented 1 year ago

Prerequisites

Issue

Good day everyone.

I'm using simple protocol named as JSDOC Long story short - on every request, server response with code 200 but JSON body may contain different content (payload, errors, etc.)

I should make schema for this multiply content.

With OpenAPI we can use components:

openapi: 3.0.0
info:
  title: My API
  version: 1.0.0
paths:
  /example:
    get:
      summary: Example JSEND endpoint
      responses:
        '200':
          description: Successful/Fail/Error response with different structures
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/ResponseSchema1'
                  - $ref: '#/components/schemas/ResponseSchema2'
                  - $ref: '#/components/schemas/ResponseSchema3'

components:
  schemas:
    ResponseSchema1:
      type: object
      properties:
        message:
          type: string
        additionalField1:
          type: string

    ResponseSchema2:
      type: object
      properties:
        message:
          type: string
        additionalField2:
          type: integer
    ResponseSchema3:
      type: object
      properties:
        message:
          type: string
        additionalField2:
          type: integer

You can try it here

Sorry, but I could not find how to realise it.

Please, help me find an answer to this question.

P.S. Do you have chat or community about this repository?

Uzlopak commented 1 year ago

https://discord.com/channels/725613461949906985/725613462373269567

Hydrock commented 1 year ago

Perhaps it is possible to specify a scheme with a raw format? this option would suit me as well. For example, I would describe the open api scheme in the form of a template string and the plugin would poison this scheme in the ui bypassing object parsing

So far I have decided to describe the bodies of the 200 201 and 202 response codes. I will know that in fact all the answers are with code 200 - but this solution looks ugly.

Uzlopak commented 1 year ago

Actually you should use the schema option if possible https://fastify.dev/docs/latest/Reference/Validation-and-Serialization