Skip to content

Commit

Permalink
Pass annotated origin type to build OpenAPI docs
Browse files Browse the repository at this point in the history
  • Loading branch information
tyzhnenko committed Jan 24, 2024
1 parent bec8e14 commit f9951ea
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
5 changes: 5 additions & 0 deletions blacksheep/server/openapi/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from datetime import date, datetime
from enum import Enum, IntEnum
from typing import Any, Dict, Iterable, List, Optional, Tuple, Type, Union
from typing import _AnnotatedAlias as AnnotatedAlias
from typing import _GenericAlias as GenericAlias
from typing import get_type_hints
from uuid import UUID
Expand Down Expand Up @@ -649,6 +650,10 @@ def _get_schema_by_type(
if stored_ref: # pragma: no cover
return stored_ref

if isinstance(object_type, AnnotatedAlias):
# Replace Annotated object type with the original type
object_type = getattr(object_type, "__origin__")

if self._can_handle_class_type(object_type):
return self._get_schema_for_class(object_type)

Expand Down
50 changes: 49 additions & 1 deletion tests/test_openapi_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@
)
from pydantic import VERSION as PYDANTIC_LIB_VERSION
from pydantic import BaseModel, HttpUrl
from pydantic.types import NegativeFloat, PositiveInt, condecimal, confloat, conint
from pydantic.types import (
UUID4,
NegativeFloat,
PositiveInt,
condecimal,
confloat,
conint,
)

from blacksheep.server.application import Application
from blacksheep.server.bindings import FromForm
Expand Down Expand Up @@ -77,6 +84,7 @@ class PydExampleWithSpecificTypes(BaseModel):
class PydCat(BaseModel):
id: int
name: str
childs: list[UUID4]


class PydPaginatedSetOfCat(BaseModel):
Expand Down Expand Up @@ -1073,6 +1081,7 @@ def home() -> PydPaginatedSetOfCat:
required:
- id
- name
- childs
properties:
id:
type: integer
Expand All @@ -1081,6 +1090,13 @@ def home() -> PydPaginatedSetOfCat:
name:
type: string
nullable: false
childs:
type: array
nullable: false
items:
type: string
format: uuid
nullable: false
PydPaginatedSetOfCat:
type: object
required:
Expand Down Expand Up @@ -1141,6 +1157,7 @@ def home() -> PydTypeWithChildModels:
required:
- id
- name
- childs
properties:
id:
type: integer
Expand All @@ -1149,6 +1166,13 @@ def home() -> PydTypeWithChildModels:
name:
type: string
nullable: false
childs:
type: array
nullable: false
items:
type: string
format: uuid
nullable: false
PydPaginatedSetOfCat:
type: object
required:
Expand Down Expand Up @@ -1230,6 +1254,7 @@ def home() -> PaginatedSet[PydCat]:
required:
- id
- name
- childs
properties:
id:
type: integer
Expand All @@ -1238,6 +1263,13 @@ def home() -> PaginatedSet[PydCat]:
name:
type: string
nullable: false
childs:
type: array
nullable: false
items:
type: string
format: uuid
nullable: false
PaginatedSetOfPydCat:
type: object
required:
Expand Down Expand Up @@ -1726,6 +1758,7 @@ def home() -> PydResponse[PydCat]:
required:
- id
- name
- childs
properties:
id:
type: integer
Expand All @@ -1734,6 +1767,13 @@ def home() -> PydResponse[PydCat]:
name:
type: string
nullable: false
childs:
type: array
nullable: false
items:
type: string
format: uuid
nullable: false
Error:
type: object
required:
Expand Down Expand Up @@ -1783,6 +1823,7 @@ def home() -> PydResponse[PydCat]:
required:
- id
- name
- childs
properties:
id:
type: integer
Expand All @@ -1791,6 +1832,13 @@ def home() -> PydResponse[PydCat]:
name:
type: string
nullable: false
childs:
type: array
nullable: false
items:
type: string
format: uuid
nullable: false
Error:
type: object
required:
Expand Down

0 comments on commit f9951ea

Please sign in to comment.