Issue 1:
Describe the bug

I want to extract enums from model schema to separate definition, and the solution is:

from enum import Enum

from pydantic import BaseModel
from devtools import debug

class HelloType(str, Enum):
World = 'World',
Friend = 'Friend',

class HelloEnum(BaseModel):
__root__: HelloType

class Foo(BaseModel):
bar: HelloEnum

debug(HelloEnum.schema())
debug(Foo.schema())

Output:

./main.py <module>
HelloEnum.schema(): {
'title': 'HelloEnum',
'enum': [
'World',
'Friend',
],
'type': 'string',
} (dict) len=3
./main.py <module>
Foo.schema(): {
'title': 'Foo',
'type': 'object',
'properties': {
'bar': {
'$ref': '#/definitions/HelloEnum',
},
},
'required': ['bar'],
'definitions': {
'HelloEnum': {
'title': 'HelloEnum',
'enum': [
'World',
'Friend',
],
'type': 'string',
},
},
} (dict) len=5

But I can't use HelloEnum in Query parameters:

@app.get('/')
async def root(hello_type: HelloEnum = Query(...)):
return {'message': f'Hello {hello_type}'}

And I get the error:

File "./main.py", line 28, in <module>
async def root(hello_type: HelloEnum = Query(...)):
File "<..>/python3.8/site-packages/fastapi/routing.py", line 514, in decorator
self.add_api_route(
File "<..>/python3.8/site-packages/fastapi/routing.py", line 453, in add_api_route
route = route_class(
File "<..>/python3.8/site-packages/fastapi/routing.py", line 370, in __init__
self.dependant = get_dependant(path=self.path_format, call=self.endpoint)
File "<..>/python3.8/site-packages/fastapi/dependencies/utils.py", line 308, in get_dependant
assert isinstance(
AssertionError: Param: hello_type can only be a request body, using Body(...)

To Reproduce

Create a file with:

from enum import Enum

from fastapi import FastAPI, Query
from pydantic import BaseModel

app = FastAPI()

class HelloType(str, Enum):
World = 'World',
Friend = 'Friend',

class HelloEnum(BaseModel):
__root__: HelloType

@app.get('/')
async def root(hello_type: HelloEnum = Query(...)):
return {'message': f'Hello {hello_type}'}

Run app.

It trows an exception AssertionError: Param: hello_type can only be a request body, using Body(...).

But I expected it to run successfully.

Expected behavior

Run app successfully, and in openapi schema enum extracted in the separate definition.

Environment

macOS 10.15.4

FastAPI 0.54.2

Python 3.8.2

Issue 2:
Describe the bug

https://pydantic-docs.helpmanual.io/usage/models/#custom-root-types

Pydantic allows to create models with only __root__ field. In such scenario the model behaves as transparent wrapper for this single type.

When such model is used in response (request also?) fastapi does not treat it correctly and renders it as object with __root__ field.

Object is treated correctly by pydantic itself.

To Reproduce

from typing import List
from fastapi import FastAPI
from pydantic.main import BaseModel

app = FastAPI()

class RootTestClass(BaseModel):
__root__: List[str]

@app.get("/")
async def root():
response = RootTestClass(__root__=['a', 'b', 'c'])
print(response.json()) # ["a", "b", "c"] so it's OK
print(RootTestClass.schema()) # {'title': 'RootTestClass', 'type': 'array', 'items': {'type': 'string'}} this is also OK
return response # Wrong value in http response

Expected behavior

The response should be:

["a", "b", "c"]

but at the moment is:

{"__root__":["a","b","c"]}

Screenshots

N/A

Environment

OS: Linux

FastAPI Version: 0.47.1

Python version: Python 3.7.5

Additional context

N/A