question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] NewType breaks OpenAPI generation

See original GitHub issue

Describe the bug FastAPI fails to generate openapi.json when the schema contains types declared with typing.NewType

To Reproduce Steps to reproduce the behavior:

  1. 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}"
  1. Run the app:
uvloop failme:app
  1. Navigate to http://127.0.0.1:8000/docs
  2. 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 image

Environment:

  • OS: Linux
  • FastAPI Version 0.35.0
  • Python version: 3.7.3

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
zamiramircommented, Sep 3, 2019

FYI: Resolved in #463

4reactions
dmontagucommented, Sep 3, 2019

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found