Skip to content

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

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 3 commits into from
Mar 27, 2025
Merged

Conversation

omikader
Copy link
Contributor

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
@luolingchun luolingchun merged commit a1cfc69 into luolingchun:master Mar 27, 2025
10 checks passed
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