[BUG] NewType breaks OpenAPI generation
See original GitHub issueDescribe the bug
FastAPI fails to generate openapi.json
when the schema contains types declared with typing.NewType
To Reproduce Steps to reproduce the behavior:
- Create a file
failme.py
with contents:
from typing import NewType
from fastapi import FastAPI, Query, Body
app = FastAPI()
Identifier = NewType("Identifier", int)
@app.post("/inpath/{num}")
async def inpath(num: Identifier) -> str:
return f"2*num={2*num}"
@app.post("/inquery/")
async def inquery(num: Identifier = Query(...)) -> str:
return f"2*num={2*num}"
@app.post("/inbody/")
async def inbody(num: Identifier = Body(...)) -> str:
return f"2*num={2*num}"
- Run the app:
uvloop failme:app
- Navigate to http://127.0.0.1:8000/docs
- The documentation is not rendered in the web page. The command line shows the error:
[...]
File ".../lib/python3.7/site-packages/fastapi/applications.py", line 87, in openapi
return JSONResponse(self.openapi())
File ".../lib/python3.7/site-packages/fastapi/applications.py", line 79, in openapi
openapi_prefix=self.openapi_prefix,
File ".../lib/python3.7/site-packages/fastapi/openapi/utils.py", line 254, in get_openapi
result = get_openapi_path(route=route, model_name_map=model_name_map)
File ".../lib/python3.7/site-packages/fastapi/openapi/utils.py", line 163, in get_openapi_path
all_route_params=all_route_params
File ".../lib/python3.7/site-packages/fastapi/openapi/utils.py", line 87, in get_openapi_operation_parameters
"schema": field_schema(param, model_name_map={})[0],
File "pydantic/schema.py", line 288, in pydantic.schema.field_schema
File "pydantic/schema.py", line 518, in pydantic.schema.field_type_schema
File "pydantic/schema.py", line 745, in pydantic.schema.field_singleton_schema
TypeError: issubclass() arg 1 must be a class
Expected behavior The OpenAPI documentation to be rendered rendered.
Screenshots
Environment:
- OS: Linux
- FastAPI Version 0.35.0
- Python version: 3.7.3
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
An adventure in OpenAPI V3 code generation | Mux blog
OpenAPI Generator is a comprehensive Java application which can generate client and server side code from your OpenAPI models.
Read more >Upgrading from tsoa 2.5 - GitHub Pages
This is a breaking change for those who were using OpenAPI 3, but it actually brings tsoa into parity with how we were...
Read more >Open-generator-cli problems, or Swagger/OpenApi ...
So, I tried java -jar openapi-generator-cli-3.3.4.jar generate -g elm -i swagger.json. instead. The code is slightly different, for example, no extra type ...
Read more >Changelog - R Plumber
Breaking changes. First line of endpoint comments interpreted as OpenAPI 'summary' field and subsequent comment lines interpreted as 'description' field.
Read more >ng-openapi-gen - npm
ng-openapi-gen: An OpenAPI 3 code generator for Angular ... This project is a NPM module that generates model interfaces and web service clients ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
FYI: Resolved in #463
@Victor-Savu @xyb #423 was replaced with a different PR upgrading to newer pydantic. It is now merged; as of 0.38 NewType works for schema generation.