Skip to content
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

fix(models): enable 'populate_by_name' to alias fields correctly #221

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

omikader
Copy link

Summary

This PR adds "populate_by_name": True to the model_config for the SecurityScheme model

Context

When creating the application, we can define supported security schemes using plain dicts or the SecurityScheme object. The SecurityScheme object is more typesafe but must eventually be dumped to JSON in order for the API docs to be generated.

app = OpenAPI(
    "app",
    security_schemes={
        "jwt": {"type": "http", "scheme": "bearer"},
        "key": {"type": "apiKey", "in": "header", "name": "X-APP-API-KEY"},
    },
)

OR

app = OpenAPI(
    "app",
    security_schemes={
        "jwt": SecurityScheme(type="http", scheme="bearer"},
        "key": SecurityScheme(type="apiKey", security_scheme_in="header", name="X-APP-API-KEY"),
    },
)

The in keyword is reserved in Python which is why it is currently aliased to security_scheme_in however the model_dump invocation doesn't actually alias it correctly unless populate_by_name is enabled.

self.spec_json = self.spec.model_dump(mode="json", by_alias=True, exclude_unset=True, warnings=False)

Checklist:

  • Run pytest tests and no failed.
  • Run ruff check flask_openapi3 tests examples and no failed.
  • Run mypy flask_openapi3 and no failed.
  • Run mkdocs serve and no failed.

@omikader omikader changed the title fix(security-scheme): enable 'populate_by_name' so 'in' is aliased correctly fix(models): enable 'populate_by_name' so 'in' is aliased correctly Mar 13, 2025
@omikader omikader changed the title fix(models): enable 'populate_by_name' so 'in' is aliased correctly fix(models): enable 'populate_by_name' to alias fields correctly Mar 13, 2025
@@ -53,3 +53,8 @@ class Schema(BaseModel):
externalDocs: Optional[ExternalDocumentation] = None
example: Optional[Any] = None
deprecated: Optional[bool] = None

model_config = {
"extra": "allow",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Schema not need "extra": "allow"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants