Browse Source

Update OpenAPI models, supporting recursive models and extensions (#3628)

pull/3629/head
Sebastián Ramírez 4 years ago
committed by GitHub
parent
commit
97fa743ecb
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 117
      fastapi/openapi/models.py

117
fastapi/openapi/models.py

@ -30,11 +30,17 @@ class Contact(BaseModel):
url: Optional[AnyUrl] = None url: Optional[AnyUrl] = None
email: Optional[EmailStr] = None email: Optional[EmailStr] = None
class Config:
extra = "allow"
class License(BaseModel): class License(BaseModel):
name: str name: str
url: Optional[AnyUrl] = None url: Optional[AnyUrl] = None
class Config:
extra = "allow"
class Info(BaseModel): class Info(BaseModel):
title: str title: str
@ -44,18 +50,27 @@ class Info(BaseModel):
license: Optional[License] = None license: Optional[License] = None
version: str version: str
class Config:
extra = "allow"
class ServerVariable(BaseModel): class ServerVariable(BaseModel):
enum: Optional[List[str]] = None enum: Optional[List[str]] = None
default: str default: str
description: Optional[str] = None description: Optional[str] = None
class Config:
extra = "allow"
class Server(BaseModel): class Server(BaseModel):
url: Union[AnyUrl, str] url: Union[AnyUrl, str]
description: Optional[str] = None description: Optional[str] = None
variables: Optional[Dict[str, ServerVariable]] = None variables: Optional[Dict[str, ServerVariable]] = None
class Config:
extra = "allow"
class Reference(BaseModel): class Reference(BaseModel):
ref: str = Field(..., alias="$ref") ref: str = Field(..., alias="$ref")
@ -73,13 +88,19 @@ class XML(BaseModel):
attribute: Optional[bool] = None attribute: Optional[bool] = None
wrapped: Optional[bool] = None wrapped: Optional[bool] = None
class Config:
extra = "allow"
class ExternalDocumentation(BaseModel): class ExternalDocumentation(BaseModel):
description: Optional[str] = None description: Optional[str] = None
url: AnyUrl url: AnyUrl
class Config:
extra = "allow"
class SchemaBase(BaseModel): class Schema(BaseModel):
ref: Optional[str] = Field(None, alias="$ref") ref: Optional[str] = Field(None, alias="$ref")
title: Optional[str] = None title: Optional[str] = None
multipleOf: Optional[float] = None multipleOf: Optional[float] = None
@ -98,13 +119,13 @@ class SchemaBase(BaseModel):
required: Optional[List[str]] = None required: Optional[List[str]] = None
enum: Optional[List[Any]] = None enum: Optional[List[Any]] = None
type: Optional[str] = None type: Optional[str] = None
allOf: Optional[List[Any]] = None allOf: Optional[List["Schema"]] = None
oneOf: Optional[List[Any]] = None oneOf: Optional[List["Schema"]] = None
anyOf: Optional[List[Any]] = None anyOf: Optional[List["Schema"]] = None
not_: Optional[Any] = Field(None, alias="not") not_: Optional["Schema"] = Field(None, alias="not")
items: Optional[Any] = None items: Optional["Schema"] = None
properties: Optional[Dict[str, Any]] = None properties: Optional[Dict[str, "Schema"]] = None
additionalProperties: Optional[Union[Dict[str, Any], bool]] = None additionalProperties: Optional[Union["Schema", Reference, bool]] = None
description: Optional[str] = None description: Optional[str] = None
format: Optional[str] = None format: Optional[str] = None
default: Optional[Any] = None default: Optional[Any] = None
@ -121,22 +142,15 @@ class SchemaBase(BaseModel):
extra: str = "allow" extra: str = "allow"
class Schema(SchemaBase):
allOf: Optional[List[SchemaBase]] = None
oneOf: Optional[List[SchemaBase]] = None
anyOf: Optional[List[SchemaBase]] = None
not_: Optional[SchemaBase] = Field(None, alias="not")
items: Optional[SchemaBase] = None
properties: Optional[Dict[str, SchemaBase]] = None
additionalProperties: Optional[Union[Dict[str, Any], bool]] = None
class Example(BaseModel): class Example(BaseModel):
summary: Optional[str] = None summary: Optional[str] = None
description: Optional[str] = None description: Optional[str] = None
value: Optional[Any] = None value: Optional[Any] = None
externalValue: Optional[AnyUrl] = None externalValue: Optional[AnyUrl] = None
class Config:
extra = "allow"
class ParameterInType(Enum): class ParameterInType(Enum):
query = "query" query = "query"
@ -147,12 +161,14 @@ class ParameterInType(Enum):
class Encoding(BaseModel): class Encoding(BaseModel):
contentType: Optional[str] = None contentType: Optional[str] = None
# Workaround OpenAPI recursive reference, using Any headers: Optional[Dict[str, Union["Header", Reference]]] = None
headers: Optional[Dict[str, Union[Any, Reference]]] = None
style: Optional[str] = None style: Optional[str] = None
explode: Optional[bool] = None explode: Optional[bool] = None
allowReserved: Optional[bool] = None allowReserved: Optional[bool] = None
class Config:
extra = "allow"
class MediaType(BaseModel): class MediaType(BaseModel):
schema_: Optional[Union[Schema, Reference]] = Field(None, alias="schema") schema_: Optional[Union[Schema, Reference]] = Field(None, alias="schema")
@ -160,6 +176,9 @@ class MediaType(BaseModel):
examples: Optional[Dict[str, Union[Example, Reference]]] = None examples: Optional[Dict[str, Union[Example, Reference]]] = None
encoding: Optional[Dict[str, Encoding]] = None encoding: Optional[Dict[str, Encoding]] = None
class Config:
extra = "allow"
class ParameterBase(BaseModel): class ParameterBase(BaseModel):
description: Optional[str] = None description: Optional[str] = None
@ -175,6 +194,9 @@ class ParameterBase(BaseModel):
# Serialization rules for more complex scenarios # Serialization rules for more complex scenarios
content: Optional[Dict[str, MediaType]] = None content: Optional[Dict[str, MediaType]] = None
class Config:
extra = "allow"
class Parameter(ParameterBase): class Parameter(ParameterBase):
name: str name: str
@ -185,16 +207,14 @@ class Header(ParameterBase):
pass pass
# Workaround OpenAPI recursive reference
class EncodingWithHeaders(Encoding):
headers: Optional[Dict[str, Union[Header, Reference]]] = None
class RequestBody(BaseModel): class RequestBody(BaseModel):
description: Optional[str] = None description: Optional[str] = None
content: Dict[str, MediaType] content: Dict[str, MediaType]
required: Optional[bool] = None required: Optional[bool] = None
class Config:
extra = "allow"
class Link(BaseModel): class Link(BaseModel):
operationRef: Optional[str] = None operationRef: Optional[str] = None
@ -204,6 +224,9 @@ class Link(BaseModel):
description: Optional[str] = None description: Optional[str] = None
server: Optional[Server] = None server: Optional[Server] = None
class Config:
extra = "allow"
class Response(BaseModel): class Response(BaseModel):
description: str description: str
@ -211,6 +234,9 @@ class Response(BaseModel):
content: Optional[Dict[str, MediaType]] = None content: Optional[Dict[str, MediaType]] = None
links: Optional[Dict[str, Union[Link, Reference]]] = None links: Optional[Dict[str, Union[Link, Reference]]] = None
class Config:
extra = "allow"
class Operation(BaseModel): class Operation(BaseModel):
tags: Optional[List[str]] = None tags: Optional[List[str]] = None
@ -220,9 +246,9 @@ class Operation(BaseModel):
operationId: Optional[str] = None operationId: Optional[str] = None
parameters: Optional[List[Union[Parameter, Reference]]] = None parameters: Optional[List[Union[Parameter, Reference]]] = None
requestBody: Optional[Union[RequestBody, Reference]] = None requestBody: Optional[Union[RequestBody, Reference]] = None
responses: Dict[str, Response] # Using Any for Specification Extensions
# Workaround OpenAPI recursive reference responses: Dict[str, Union[Response, Any]]
callbacks: Optional[Dict[str, Union[Dict[str, Any], Reference]]] = None callbacks: Optional[Dict[str, Union[Dict[str, "PathItem"], Reference]]] = None
deprecated: Optional[bool] = None deprecated: Optional[bool] = None
security: Optional[List[Dict[str, List[str]]]] = None security: Optional[List[Dict[str, List[str]]]] = None
servers: Optional[List[Server]] = None servers: Optional[List[Server]] = None
@ -246,10 +272,8 @@ class PathItem(BaseModel):
servers: Optional[List[Server]] = None servers: Optional[List[Server]] = None
parameters: Optional[List[Union[Parameter, Reference]]] = None parameters: Optional[List[Union[Parameter, Reference]]] = None
class Config:
# Workaround OpenAPI recursive reference extra = "allow"
class OperationWithCallbacks(BaseModel):
callbacks: Optional[Dict[str, Union[Dict[str, PathItem], Reference]]] = None
class SecuritySchemeType(Enum): class SecuritySchemeType(Enum):
@ -263,6 +287,9 @@ class SecurityBase(BaseModel):
type_: SecuritySchemeType = Field(..., alias="type") type_: SecuritySchemeType = Field(..., alias="type")
description: Optional[str] = None description: Optional[str] = None
class Config:
extra = "allow"
class APIKeyIn(Enum): class APIKeyIn(Enum):
query = "query" query = "query"
@ -290,6 +317,9 @@ class OAuthFlow(BaseModel):
refreshUrl: Optional[str] = None refreshUrl: Optional[str] = None
scopes: Dict[str, str] = {} scopes: Dict[str, str] = {}
class Config:
extra = "allow"
class OAuthFlowImplicit(OAuthFlow): class OAuthFlowImplicit(OAuthFlow):
authorizationUrl: str authorizationUrl: str
@ -314,6 +344,9 @@ class OAuthFlows(BaseModel):
clientCredentials: Optional[OAuthFlowClientCredentials] = None clientCredentials: Optional[OAuthFlowClientCredentials] = None
authorizationCode: Optional[OAuthFlowAuthorizationCode] = None authorizationCode: Optional[OAuthFlowAuthorizationCode] = None
class Config:
extra = "allow"
class OAuth2(SecurityBase): class OAuth2(SecurityBase):
type_ = Field(SecuritySchemeType.oauth2, alias="type") type_ = Field(SecuritySchemeType.oauth2, alias="type")
@ -337,7 +370,11 @@ class Components(BaseModel):
headers: Optional[Dict[str, Union[Header, Reference]]] = None headers: Optional[Dict[str, Union[Header, Reference]]] = None
securitySchemes: Optional[Dict[str, Union[SecurityScheme, Reference]]] = None securitySchemes: Optional[Dict[str, Union[SecurityScheme, Reference]]] = None
links: Optional[Dict[str, Union[Link, Reference]]] = None links: Optional[Dict[str, Union[Link, Reference]]] = None
callbacks: Optional[Dict[str, Union[Dict[str, PathItem], Reference]]] = None # Using Any for Specification Extensions
callbacks: Optional[Dict[str, Union[Dict[str, PathItem], Reference, Any]]] = None
class Config:
extra = "allow"
class Tag(BaseModel): class Tag(BaseModel):
@ -345,13 +382,25 @@ class Tag(BaseModel):
description: Optional[str] = None description: Optional[str] = None
externalDocs: Optional[ExternalDocumentation] = None externalDocs: Optional[ExternalDocumentation] = None
class Config:
extra = "allow"
class OpenAPI(BaseModel): class OpenAPI(BaseModel):
openapi: str openapi: str
info: Info info: Info
servers: Optional[List[Server]] = None servers: Optional[List[Server]] = None
paths: Dict[str, PathItem] # Using Any for Specification Extensions
paths: Dict[str, Union[PathItem, Any]]
components: Optional[Components] = None components: Optional[Components] = None
security: Optional[List[Dict[str, List[str]]]] = None security: Optional[List[Dict[str, List[str]]]] = None
tags: Optional[List[Tag]] = None tags: Optional[List[Tag]] = None
externalDocs: Optional[ExternalDocumentation] = None externalDocs: Optional[ExternalDocumentation] = None
class Config:
extra = "allow"
Schema.update_forward_refs()
Operation.update_forward_refs()
Encoding.update_forward_refs()

Loading…
Cancel
Save