OpenAPI: Conditional schema of one parameter depending on value of another – Openapi

by
Alexei Petrov
conditional-statements openapi-generator swagger-ui

Quick Fix: Leverage inheritance with a discriminator to specify the type of an object within a hierarchy. Create multiple schemas, each with a mode and a value field of different types, depending on the mode. Use the discriminator to map the mode to the appropriate schema.

The Problem:

In an OpenAPI specification, the type of the input parameter should be an integer if the mode parameter is of value modeA and a string if the mode parameter is modeB. How to define this conditional schema for one parameter depending on the value of another parameter.

The Solutions:

Solution 1: Using Inheritance with Discriminator

To model the conditional schema of one parameter (input) depending on the value of another parameter (mode), you can use inheritance with a discriminator in your OpenAPI specification. Here’s how you can achieve this:

  1. Define three schemas:

    • VariantWithInt: Represents the schema when mode is "modeA". It contains an integer field called value.
    • VariantWithString: Represents the schema when mode is "modeB". It contains a string field called value.
    • VariantWithStringOrInt: This schema acts as the parent schema and uses the discriminator to specify the type of object it represents. It has a discriminator property that references the mode field and maps it to the respective child schemas (VariantWithInt and VariantWithString).
  2. In the parameters section of your API operation, reference the VariantWithStringOrInt schema for the input parameter. This indicates that the input parameter can be of type integer or string, depending on the value of the mode parameter.

Here’s an example of how the OpenAPI specification would look like:

openapi: 3.0.0
paths:
  /mypath:
    get:
      parameters:
        - in: query
          name: input
          schema:
            $ref: '#/components/schemas/VariantWithStringOrInt'
        - in: query
          name: mode
          schema:
            type: string
            enum: [modeA, modeB]
components:
  schemas:
    VariantWithInt:
      required:
        - mode
        - value
      type: object
      properties:
        value:
          type: integer
          format: int64
        mode:
          type: string
          readOnly: true
    VariantWithString:
      required:
        - mode
        - value
      type: object
      properties:
        value:
          type: string
        mode:
          type: string
          readOnly: true
    VariantWithStringOrInt:
      discriminator:
        propertyName: mode
        mapping:
          A: VariantWithString
          B: VariantWithInt
      oneOf:
        - $ref: '#/components/schemas/VariantWithString'
        - $ref: '#/components/schemas/VariantWithInt'
      properties:
        someCommonProp:
          type: string
        anotherCommonProp:
          type: string

This approach allows you to define the schema of the input parameter dynamically based on the value of the mode parameter. When generating the code from this OpenAPI specification, it will typically create classes or data structures that represent the defined schemas. This enables you to handle the different types of input values appropriately in your API implementation.

Q&A

How to make type of input parameter an integer if mode is modeA and string if mode is modeB in OpenAPI?

Use inheritance with a discriminator – the discriminator tells you the type of your object in the hierarchy.

Is oneOf a valid approach for conditional schema?

No, oneOf is not a valid approach for conditional schema in OpenAPI.

Video Explanation:

The following video, titled "Fine-tuning GPT-3 with OpenAI API and W&B: A Tutorial Using ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

... other/openai --- ⏳ Timestamps ⏳ 00:00 Intro 01:17 Misconceptions about training GPT-3 04:19 Colab notebook for fine-tuning 07:16 Exploring ...