Skip to content

feat(event_handler): add OpenAPI extensions #4703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Jul 15, 2024
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e97f6ee
Initial commit OpenAPI Extensions
leandrodamascena Jul 8, 2024
3fceac7
Polishing the PR with best practicies - Comments
leandrodamascena Jul 8, 2024
84d035a
Polishing the PR with best practicies - Tests
leandrodamascena Jul 8, 2024
8a111d2
Polishing the PR with best practicies - make pydanticv2 happy
leandrodamascena Jul 8, 2024
270516f
Polishing the PR with best practicies - using model_validator to be m…
leandrodamascena Jul 8, 2024
771c44e
Temporary mypy disabling
leandrodamascena Jul 8, 2024
8195bda
Make mypy happy?
leandrodamascena Jul 8, 2024
657fed3
Make mypy happy?
leandrodamascena Jul 8, 2024
fec654f
Merge branch 'develop' into feat/add-openapi-extension
leandrodamascena Jul 8, 2024
06c8e23
Polishing the PR with best practicies - adding e2e tests
leandrodamascena Jul 8, 2024
aa0e0d3
Adding docstring
leandrodamascena Jul 9, 2024
c0a098b
Adding documentation
leandrodamascena Jul 9, 2024
67ba07a
Merge branch 'develop' into feat/add-openapi-extension
leandrodamascena Jul 9, 2024
83ecc37
Addressing Simon's feedback
leandrodamascena Jul 10, 2024
56497f9
Addressing Simon's feedback
leandrodamascena Jul 10, 2024
c917877
Addressing Simon's feedback
leandrodamascena Jul 10, 2024
e81deb7
Merge branch 'develop' into feat/add-openapi-extension
leandrodamascena Jul 10, 2024
b8390b0
Adding more tests
leandrodamascena Jul 10, 2024
319b3d4
Adding more tests
leandrodamascena Jul 10, 2024
d8b62f6
Merge branch 'develop' into feat/add-openapi-extension
leandrodamascena Jul 10, 2024
8d3f9c3
Adding more tests
leandrodamascena Jul 10, 2024
7d877bb
Merge branch 'develop' into feat/add-openapi-extension
leandrodamascena Jul 11, 2024
1e381d0
Merge branch 'develop' into feat/add-openapi-extension
leandrodamascena Jul 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Polishing the PR with best practicies - make pydanticv2 happy
  • Loading branch information
leandrodamascena committed Jul 8, 2024
commit 8a111d2438e4cef3b239adb06febc2f39c14a8f7
92 changes: 61 additions & 31 deletions aws_lambda_powertools/event_handler/openapi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,6 @@
"""


class OpenapiExtensions(BaseModel):
"""OpenAPI extensions, see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#specification-extensions"""

openapi_extensions: Optional[Dict[str, Any]] = None

if PYDANTIC_V2:

@parser_openapi_extension()
def serialize(self):
# If the 'openapi_extensions' field is not None, return it
if self.openapi_extensions:
return self.openapi_extensions

else:

# If the 'openapi_extensions' field is present in the 'values' dictionary,
# update the 'values' dictionary with the contents of 'openapi_extensions',
# and then remove the 'openapi_extensions' field from the 'values' dictionary
@parser_openapi_extension(pre=False, allow_reuse=True)
def check_json(cls, values):
if values.get("openapi_extensions"):
values.update(values["openapi_extensions"])
del values["openapi_extensions"]
return values


# https://swagger.io/specification/#contact-object
class Contact(BaseModel):
name: Optional[str] = None
Expand Down Expand Up @@ -103,16 +77,33 @@


# https://swagger.io/specification/#server-object
class Server(OpenapiExtensions):
class Server(BaseModel):
url: Union[AnyUrl, str]
description: Optional[str] = None
variables: Optional[Dict[str, ServerVariable]] = None
openapi_extensions: Optional[Dict[str, Any]] = None

if PYDANTIC_V2:
model_config = {"extra": "allow"}

@parser_openapi_extension()

Check warning on line 89 in aws_lambda_powertools/event_handler/openapi/models.py

View check run for this annotation

Codecov / codecov/patch

aws_lambda_powertools/event_handler/openapi/models.py#L89

Added line #L89 was not covered by tests
def serialize(self):
# If the 'openapi_extensions' field is not None, return it
if self.openapi_extensions:
return self.openapi_extensions

Check warning on line 93 in aws_lambda_powertools/event_handler/openapi/models.py

View check run for this annotation

Codecov / codecov/patch

aws_lambda_powertools/event_handler/openapi/models.py#L93

Added line #L93 was not covered by tests

else:

# If the 'openapi_extensions' field is present in the 'values' dictionary,
# update the 'values' dictionary with the contents of 'openapi_extensions',
# and then remove the 'openapi_extensions' field from the 'values' dictionary
@parser_openapi_extension(pre=False, allow_reuse=True)
def check_json(cls, values):
if values.get("openapi_extensions"):
values.update(values["openapi_extensions"])
del values["openapi_extensions"]
return values

class Config:
extra = "allow"

Expand Down Expand Up @@ -405,7 +396,7 @@


# https://swagger.io/specification/#operation-object
class Operation(OpenapiExtensions):
class Operation(BaseModel):
tags: Optional[List[str]] = None
summary: Optional[str] = None
description: Optional[str] = None
Expand All @@ -419,12 +410,23 @@
deprecated: Optional[bool] = None
security: Optional[List[Dict[str, List[str]]]] = None
servers: Optional[List[Server]] = None
openapi_extensions: Optional[Dict[str, Any]] = None

if PYDANTIC_V2:
model_config = {"extra": "allow"}

else:

# If the 'openapi_extensions' field is present in the 'values' dictionary,
# update the 'values' dictionary with the contents of 'openapi_extensions',
# and then remove the 'openapi_extensions' field from the 'values' dictionary
@parser_openapi_extension(pre=False, allow_reuse=True)
def check_json(cls, values):
if values.get("openapi_extensions"):
values.update(values["openapi_extensions"])
del values["openapi_extensions"]

Check warning on line 427 in aws_lambda_powertools/event_handler/openapi/models.py

View check run for this annotation

Codecov / codecov/patch

aws_lambda_powertools/event_handler/openapi/models.py#L426-L427

Added lines #L426 - L427 were not covered by tests
return values

class Config:
extra = "allow"

Expand Down Expand Up @@ -462,15 +464,32 @@
openIdConnect = "openIdConnect"


class SecurityBase(OpenapiExtensions):
class SecurityBase(BaseModel):
type_: SecuritySchemeType = Field(alias="type")
description: Optional[str] = None
openapi_extensions: Optional[Dict[str, Any]] = None

if PYDANTIC_V2:
model_config = {"extra": "allow", "populate_by_name": True}

@parser_openapi_extension()

Check warning on line 475 in aws_lambda_powertools/event_handler/openapi/models.py

View check run for this annotation

Codecov / codecov/patch

aws_lambda_powertools/event_handler/openapi/models.py#L475

Added line #L475 was not covered by tests
def serialize(self):
# If the 'openapi_extensions' field is not None, return it
if self.openapi_extensions:
return self.openapi_extensions

Check warning on line 479 in aws_lambda_powertools/event_handler/openapi/models.py

View check run for this annotation

Codecov / codecov/patch

aws_lambda_powertools/event_handler/openapi/models.py#L479

Added line #L479 was not covered by tests

else:

# If the 'openapi_extensions' field is present in the 'values' dictionary,
# update the 'values' dictionary with the contents of 'openapi_extensions',
# and then remove the 'openapi_extensions' field from the 'values' dictionary
@parser_openapi_extension(pre=False, allow_reuse=True)
def check_json(cls, values):
if values.get("openapi_extensions"):
values.update(values["openapi_extensions"])
del values["openapi_extensions"]
return values

class Config:
extra = "allow"
allow_population_by_field_name = True
Expand Down Expand Up @@ -560,7 +579,7 @@


# https://swagger.io/specification/#components-object
class Components(OpenapiExtensions):
class Components(BaseModel):
schemas: Optional[Dict[str, Union[Schema, Reference]]] = None
responses: Optional[Dict[str, Union[Response, Reference]]] = None
parameters: Optional[Dict[str, Union[Parameter, Reference]]] = None
Expand All @@ -583,7 +602,7 @@


# https://swagger.io/specification/#openapi-object
class OpenAPI(OpenapiExtensions):
class OpenAPI(BaseModel):
openapi: str
info: Info
jsonSchemaDialect: Optional[str] = None
Expand All @@ -595,12 +614,23 @@
security: Optional[List[Dict[str, List[str]]]] = None
tags: Optional[List[Tag]] = None
externalDocs: Optional[ExternalDocumentation] = None
openapi_extensions: Optional[Dict[str, Any]] = None

if PYDANTIC_V2:
model_config = {"extra": "allow"}

else:

# If the 'openapi_extensions' field is present in the 'values' dictionary,
# update the 'values' dictionary with the contents of 'openapi_extensions',
# and then remove the 'openapi_extensions' field from the 'values' dictionary
@parser_openapi_extension(pre=False, allow_reuse=True)
def check_json(cls, values):
if values.get("openapi_extensions"):
values.update(values["openapi_extensions"])
del values["openapi_extensions"]

Check warning on line 631 in aws_lambda_powertools/event_handler/openapi/models.py

View check run for this annotation

Codecov / codecov/patch

aws_lambda_powertools/event_handler/openapi/models.py#L630-L631

Added lines #L630 - L631 were not covered by tests
return values

class Config:
extra = "allow"

Expand Down
Loading