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

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

Open
2 tasks done
Hydrock opened this issue Aug 1, 2023 · 3 comments
Open
2 tasks done

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

Hydrock opened this issue Aug 1, 2023 · 3 comments
Labels
help wanted Help the community by contributing to this issue

Comments

@Hydrock
Copy link

Hydrock commented Aug 1, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

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
Copy link
Contributor

Uzlopak commented Aug 1, 2023

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

@Hydrock
Copy link
Author

Hydrock commented Aug 1, 2023

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
Copy link
Contributor

Uzlopak commented Aug 9, 2023

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

@Uzlopak Uzlopak added the help wanted Help the community by contributing to this issue label Aug 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Help the community by contributing to this issue
Projects
None yet
Development

No branches or pull requests

2 participants