Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Incorrect generation of openapi.json with duplicated name Enum class #2627

Closed
9 tasks done
mnixry opened this issue Jan 10, 2021 · 4 comments
Closed
9 tasks done

Incorrect generation of openapi.json with duplicated name Enum class #2627

mnixry opened this issue Jan 10, 2021 · 4 comments
Labels
question Question or problem question-migrate

Comments

@mnixry
Copy link

mnixry commented Jan 10, 2021

First check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.
  • After submitting this, I commit to one of:
    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
    • I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
    • Implement a Pull Request for a confirmed bug.

Example

Here's a self-contained, minimal, reproducible, example with my use case:

main.py

from fastapi import FastAPI

import router1
import router2

app = FastAPI()

app.include_router(router1.router, prefix="/r1")
app.include_router(router2.router, prefix="/r2")

router1.py

from enum import Enum

from fastapi import APIRouter


class DuplicatedEnum(str, Enum):
    value1 = "value1"


router = APIRouter()


@router.get("/")
async def _(type: DuplicatedEnum = DuplicatedEnum.value1):
    pass

router2.py

from enum import Enum

from fastapi import APIRouter


class DuplicatedEnum(str, Enum):
    value2 = "value2"


router = APIRouter()


@router.get("/")
async def _(type: DuplicatedEnum = DuplicatedEnum.value2):
    pass

Description

  • Open the browser and call the document /redoc.
  • It raised error Error resolving $ref pointer "http://127.0.0.1:8000/openapi.json#/components/schemas/DuplicatedEnum". Token "DuplicatedEnum" does not exist.
  • After I checked openapi.json, it gives wrong parameter schema:
{
	"openapi": "3.0.2",
	"info": {
		"title": "FastAPI",
		"version": "0.1.0"
	},
	"paths": {
		"/r1/": {
			"get": {
				"summary": " ",
				"operationId": "__r1__get",
				"parameters": [{
					"required": false,
					"schema": {
						"allOf": [{
							"$ref": "#/components/schemas/DuplicatedEnum" //should be `router1__DuplicatedEnum`
						}],
						"default": "value1"
					},
					"name": "type",
					"in": "query"
				}],
				"responses": {
					"200": {
						"description": "Successful Response",
						"content": {
							"application/json": {
								"schema": {}
							}
						}
					},
					"422": {
						"description": "Validation Error",
						"content": {
							"application/json": {
								"schema": {
									"$ref": "#/components/schemas/HTTPValidationError"
								}
							}
						}
					}
				}
			}
		},
		"/r2/": {
			"get": {
				"summary": " ",
				"operationId": "__r2__get",
				"parameters": [{
					"required": false,
					"schema": {
						"allOf": [{
							"$ref": "#/components/schemas/DuplicatedEnum"
						}],
						"default": "value2"
					},
					"name": "type",
					"in": "query"
				}],
				"responses": {
					"200": {
						"description": "Successful Response",
						"content": {
							"application/json": {
								"schema": {}
							}
						}
					},
					"422": {
						"description": "Validation Error",
						"content": {
							"application/json": {
								"schema": {
									"$ref": "#/components/schemas/HTTPValidationError"
								}
							}
						}
					}
				}
			}
		}
	},
	"components": {
		"schemas": {
			"HTTPValidationError": {
				"title": "HTTPValidationError",
				"type": "object",
				"properties": {
					"detail": {
						"title": "Detail",
						"type": "array",
						"items": {
							"$ref": "#/components/schemas/ValidationError"
						}
					}
				}
			},
			"ValidationError": {
				"title": "ValidationError",
				"required": ["loc", "msg", "type"],
				"type": "object",
				"properties": {
					"loc": {
						"title": "Location",
						"type": "array",
						"items": {
							"type": "string"
						}
					},
					"msg": {
						"title": "Message",
						"type": "string"
					},
					"type": {
						"title": "Error Type",
						"type": "string"
					}
				}
			},
			"router1__DuplicatedEnum": {
				"title": "DuplicatedEnum",
				"enum": ["value1"],
				"type": "string",
				"description": "An enumeration."
			},
			"router2__DuplicatedEnum": {
				"title": "DuplicatedEnum",
				"enum": ["value2"],
				"type": "string",
				"description": "An enumeration."
			}
		}
	}
}

Environment

  • OS: [e.g. Linux / Windows / macOS]: Linux
  • FastAPI Version [e.g. 0.3.0]: 0.63.0
  • Python version: 3.9.1
@mnixry mnixry added the question Question or problem label Jan 10, 2021
@mnixry mnixry changed the title OpenAPI.json generation incorrect with same name Enum Incorrect generation of openapi.json with duplicated name Enum class Jan 10, 2021
@falkben
Copy link
Sponsor Contributor

falkben commented Jan 10, 2021

Can confirm. Workaround would be to name the the Enum classes differently. Can you submit a PR to fix?

@mnixry
Copy link
Author

mnixry commented Jan 11, 2021

Confirmed, this is the pydantic schema generate issue ( pydantic/pydantic#1857 )
However it has been fixed in pydantic/pydantic#2226 pydantic/pydantic@13a5c7d which have not been released yet. 😢

As a temporary solution, I have to degrade pydantic to 1.5.1 to avoid this problem.

@mnixry
Copy link
Author

mnixry commented Feb 26, 2021

PyDantic 1.8 Released which contains this fix

@tiangolo
Copy link
Owner

Thanks for the help here @falkben ! 👏 🙇

Thanks for reporting back and closing the issue @mnixry 👍

Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I'm checking each one in order.

@tiangolo tiangolo reopened this Feb 27, 2023
Repository owner locked and limited conversation to collaborators Feb 27, 2023
@tiangolo tiangolo converted this issue into discussion #6954 Feb 27, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

3 participants