Bruno
Bruno

Reputation: 21

Based on OpenAPI 3.x.x, I would like to match the RequestBody with the Response for a same response code '200' and a same endpoint

I am a new bee on openapi 3.0.0 or 3.1.0 I am using:

I am trying to synchronise the several elements automatically for a simple rest API description.

For a specific POST request on the /orders endpoint, I have the possibility to send 2 different bodies : SlimcollectVerify and SlimCollectPayUnsigned with different properties values. This can be achieved with the oneOf tag, and this is functional. Now depending on the body chosen, the response will be different for the same response code: '200':

. 1st problem is: when I select SlimcollectVerify or SlimCollectPayUnsigned in the body of command description to have this information synchronized in the response description part of the 200. It means I would like to get the 'response for SlimcollectVerify' if I select 'SlimcollectVerify', and 'reponse for SlimCollectPayUnsigned' if I select 'SlimCollectPayUnsigned'.

. 2nd problem is: when I select SlimcollectVerify or SlimCollectPayUnsigned in the command description I would like to have this choice synchronized in the example RequestBody AND example response part

An example of the interface is below: enter image description here

I have tried to introduce also the discriminator without success.

I have search example from the net but could not find any unfortunately. Thanks in advance for your help

Here are the components:

components:
    slimcollectVerify:
      description: A representation of slimcollectVerify
      allOf:
        # <- - $ref: '#/components/schemas/CreateOrder'
        - type: object
          properties:
            huntingSkillSCVerifiy:
              type: string
              description: The measured skill for Verify
              default: lazy
              example: adventurous
              enum:
                - clueless
                - lazy
    slimcollectPayUnsigned:
      description: A representation of slimcollectPayUnsigned
      allOf:
        # <- - $ref: '#/components/schemas/CreateOrder'
        - type: object
          properties:
            huntingSkillSCPayUnsigned:
              type: string
              description: The measured skill for PayUNsigned
              default: lazy
              example: adventurous
              enum:
                - clueless
                - lazy
                - adventurous
                - aggressive
     requestBodies:
    CreateOrder:
      content:
        application/json:
          schema:
            oneOf: 
              - $ref: '#/components/schemas/slimcollectVerify'
              - $ref: '#/components/schemas/slimcollectPayUnsigned'
            # <- discriminator:
            # <-   propertyName: createOrderItemType # <- property used to discriminate between response types
            # <- required:
            # <-   - createOrderItemType # <- it must be required
      description: Request Body of the create order
      required: true

Here are the path:

paths:
  /orders:
    post:
      tags:
        - pet
      summary: Create an order
      description: Command to create an order.
      operationId: createOrder
      responses:
        '200':
          description: successful operation
          content:
            "application/json":
              schema:
                oneOf:
                  - $ref: '#/components/schemas/slimcollectVerify'
                  - $ref: '#/components/schemas/slimcollectPayUnsigned'
                discriminator:
                  propertyName: createOrderItemType # <- property used to discriminate between response types
                required:
                  - createOrderItemType # <- it must be required
        '405':
          description: Invalid input
      requestBody:
        $ref: '#/components/requestBodies/CreateOrder'```


Upvotes: 2

Views: 269

Answers (1)

Jeremy Fiel
Jeremy Fiel

Reputation: 3262

This is a two part answer

  1. OpenAPI doesn't support a "scenario" like behavior you are describing. I'm not aware of any tooling that is able to create these scenarios without using some sort of customization. I have seen others attempt to use the examples mapping key to indicate something similar. here's an example I created for someone just the other day with a similar request. https://stackoverflow.com/a/77002583/8564731

  2. This is a feature request for Redocly, nothing to do with OpenAPI. Your schema is correct and valid. I've seen others request something similar https://github.com/Redocly/redoc/issues/2346

feel free to file a new issue with Redocly team. They are friendly and provide amazing support. www.github.com/redocly/redocly/issues

If you're keen to join the discussion about the future of OpenAPI, this and many other enhancements are currently being discussed in the OAI github discussions. https://github.com/OAI/OpenAPI-Specification/discussions/2930

Upvotes: 0

Related Questions