Skip to content

Instantly share code, notes, and snippets.

@guyarad
Last active March 20, 2024 16:09
Show Gist options
  • Save guyarad/21bdd60b5643ac0d13ae5a57c593eefc to your computer and use it in GitHub Desktop.
Save guyarad/21bdd60b5643ac0d13ae5a57c593eefc to your computer and use it in GitHub Desktop.
Simplify-oneof-anyof OpenAPI generator bug
generatorName: python
inputSpec: bug-repro/openapi.json
outputDir: bug-repro/python
generateSourceCodeOnly: true
globalProperties:
modelDocs: false
apiDocs: false
modelTests: false
apiTests: false
{
"openapi": "3.1.0",
"info": {
"title": "simplify-oneof-anyof bug repro",
"version": "0.1.0"
},
"paths": {
"/items/{item_id}": {
"put": {
"operationId": "update_item",
"summary": "Update Item",
"parameters": [
{
"in": "path",
"name": "item_id",
"required": true,
"schema": {"title": "Item Id", "type": "integer"}
},
{
"in": "query",
"name": "q",
"required": false,
"schema": {
"title": "Q",
"anyOf": [{"type": "string"}, {"type": "null"}
]
}
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {"schema": {"$ref": "#/components/schemas/Item"}}
}
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {"schema": {"$ref": "#/components/schemas/UpdateResponse"}}
}
}
}
}
}
},
"components": {
"schemas": {
"Item": {
"title": "Item",
"type": "object",
"properties": {
"is_offer": {
"title": "Is Offer",
"anyOf": [{"type": "boolean"}, {"type": "null"}]
},
"my_enum": {"anyOf": [{"$ref": "#/components/schemas/MyEnum"}, {"type": "null"}]},
"name": {"title": "Name", "type": "string"}
},
"required": ["name"]
},
"MyEnum": {
"title": "MyEnum",
"type": "string",
"enum": ["value-1", "value-2"]
},
"UpdateResponse": {
"properties": {
"id": {"title": "Item Id", "type": "integer"},
"name": {"title": "Item Name", "type": "string"}
},
"required": ["name", "id"],
"title": "UpdateResponse",
"type": "object"
}
}
}
}

Setup

Download the files in this gist to a folder called bug-repro.

If you wish to use openapi-generator repository, place bug-repro under the root of the repository.

Expected behavior (v7.2.0)

The Item model should have the following fields defined like so:

class Item(BaseModel):
    is_offer: Optional[StrictBool] = None
    my_enum: Optional[MyEnum] = None
    ...

Actual behavior (v7.3.0, v7.4.0)

OpenAPI normalizer SIMPLIFY_ONEOF_ANYOF does not work (at least for Python).

New models are introduced: ItemMyEnum, IsOffer and Q.

Item's definition looks like:

class Item(BaseModel):
    is_offer: Optional[IsOffer] = None
    my_enum: Optional[ItemMyEnum] = None
    ...

It makes usage very cumbersome:

Item(name='foo',
     my_enum=ItemMyEnum(MyEnum.VALUE_MINUS_1),
     is_offer=IsOffer(True))

Run in openapi-generator repository

From the repository root run:

./run-in-docker.sh generate -c bug-repro/generator-config.yaml

Run in a released docker

From the parent folder of bug-repro run:

OPENAPI_GENERATOR_VERSION=v7.2.0

docker run --rm -w /gen \
  -v ${PWD}:/gen \
  -u "$(id -u):$(id -g)" \
  openapitools/openapi-generator-cli:${OPENAPI_GENERATOR_VERSION} generate \
  -c bug-repro/generator-config.yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment