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:
-
Define three schemas:
- VariantWithInt: Represents the schema when
mode
is "modeA". It contains aninteger
field calledvalue
. - VariantWithString: Represents the schema when
mode
is "modeB". It contains astring
field calledvalue
. - 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 themode
field and maps it to the respective child schemas (VariantWithInt
andVariantWithString
).
- VariantWithInt: Represents the schema when
-
In the
parameters
section of your API operation, reference theVariantWithStringOrInt
schema for theinput
parameter. This indicates that theinput
parameter can be of typeinteger
orstring
, depending on the value of themode
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.
... other/openai --- ⏳ Timestamps ⏳ 00:00 Intro 01:17 Misconceptions about training GPT-3 04:19 Colab notebook for fine-tuning 07:16 Exploring ...
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.
... other/openai --- ⏳ Timestamps ⏳ 00:00 Intro 01:17 Misconceptions about training GPT-3 04:19 Colab notebook for fine-tuning 07:16 Exploring ...