Skip to content

Fix FastAPI example generation#23537

Open
Goopher wants to merge 3 commits intoOpenAPITools:masterfrom
Goopher:feature/fastapi-examples
Open

Fix FastAPI example generation#23537
Goopher wants to merge 3 commits intoOpenAPITools:masterfrom
Goopher:feature/fastapi-examples

Conversation

@Goopher
Copy link
Copy Markdown

@Goopher Goopher commented Apr 14, 2026

Fixes FastAPI server example generation so explicit OpenAPI examples are included in generated FastAPI/Pydantic metadata.

Changes:

  • Adds explicit request body examples to generated FastAPI Body(..., examples=[...]) metadata.
  • Preserves schema/property examples in generated Pydantic fields using json_schema_extra={"examples": [...]}.
  • Avoids leaking generated fallback examples like '', None, or empty array placeholders into Swagger UI.
  • Uses plural examples for FastAPI metadata to avoid example deprecation warnings.
  • Adds FastAPI generator tests covering request body examples and schema property examples.

Validation:

mvn -pl modules/openapi-generator -Dtest=PythonFastAPIServerCodegenTest -Dsurefire.failIfNoSpecifiedTests=true test

Result:

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0

### PR checklist

- [ ] Read the contribution guidelines (https://github.com/openapitools/openapi-generator/blob/master/CONTRIBUTING.md).
- [x] Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
- [ ] Run the following to build the project (https://github.com/OpenAPITools/openapi-generator#14---build-projects) and update samples:

  ./mvnw clean package || exit
  ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
  ./bin/utils/export_docs_generators.sh || exit
  (For Windows users, please run the script in WSL (https://learn.microsoft.com/en-us/windows/wsl/install))
  Commit all changed files.
  This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
  These must match the expectations made by your contribution.
  You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
  IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
- [x] File the PR against the correct branch (https://github.com/OpenAPITools/openapi-generator/wiki/Git-Branches): master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
- [ ] If your PR solves a reported issue, reference it using GitHub's linking syntax (https://docs.github.com/en/issues/tracking-your-work-with-issues/using-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword) (e.g., having "fixes #123" present in the PR descrip
  tion)
- [x] If your PR is targeting a particular programming language, @mention the technical committee (https://github.com/openapitools/openapi-generator/#62---openapi-generator-technical-committee) members, so they are more likely to review the pull request.

<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Fixes FastAPI example generation so explicit OpenAPI examples — including plural `examples` and `$ref` examples — render in generated endpoints, parameters, and models without placeholders. Also keeps trailing commas inline on generated endpoint parameters.

- **Bug Fixes**
- Request body examples: `Body(..., examples=[...])` from `application/json`, resolving requestBody and `$ref`-ed `components/examples`.
- Non-body params: `examples=[...]` with safe Python literals; avoids placeholder fallbacks in Swagger UI.
- Models/fields: `json_schema_extra={"examples": [...]}` using `example`, else first from `examples` (covers OAS 3.1).
- Formatting: keep trailing comma on the same line in generated endpoint parameters.
- Tests for body examples, schema property examples, and singular vs plural example precedence.

<sup>Written for commit 949744ad250980d3a59d3078ec9b3a78c6f6f9d6. Summary will update on new commits.</sup>

<!-- End of auto-generated description by cubic. -->

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 5 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java:686">
P2: Schema example extraction only checks `example` and ignores `examples`, which can drop explicit schema examples in generated Python metadata.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@Goopher Goopher force-pushed the feature/fastapi-examples branch from 8891f48 to 3a5bb74 Compare April 14, 2026 11:28
@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 14, 2026

Thanks for the PR but your commit (as shown in the Commits tab) is not linked to your Github account, which means this PR won't count as your contribution in https://github.com/OpenAPITools/openapi-generator/graphs/contributors.

Let me know if you need help fixing it.

Ref: https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-can-i-update-commits-that-are-not-linked-to-my-github-account

Goopher added 2 commits April 14, 2026 15:50
- Fall back to first entry of `examples` array when `example` is absent,
  fixing schema example extraction for OAS 3.1 specs.
- Add unit tests for plural and singular example precedence via
  TestableFastAPICodegen helper subclass.
- Add nickname property with examples array to test fixture.
@Goopher Goopher force-pushed the feature/fastapi-examples branch from 3a5bb74 to dedc38c Compare April 14, 2026 13:51
@Goopher
Copy link
Copy Markdown
Author

Goopher commented Apr 14, 2026

Thanks for the PR but your commit (as shown in the Commits tab) is not linked to your Github account, which means this PR won't count as your contribution in https://github.com/OpenAPITools/openapi-generator/graphs/contributors.

Let me know if you need help fixing it.

Ref: https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-can-i-update-commits-that-are-not-linked-to-my-github-account

Thanks for notifying me, fixed.

@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 14, 2026

--- a/samples/server/petstore/python-fastapi/src/openapi_server/apis/pet_api.py
+++ b/samples/server/petstore/python-fastapi/src/openapi_server/apis/pet_api.py
@@ -50,7 +50,8 @@ for _, name, _ in pkgutil.iter_modules(ns_pkg.__path__, ns_pkg.__name__ + "."):
     response_model_by_alias=True,
 )
 async def update_pet(
-    pet: Annotated[Pet, Field(description="Pet object that needs to be added to the store")] = Body(None, description="Pet object that needs to be added to the store"),
+    pet: Annotated[Pet, Field(description="Pet object that needs to be added to the store")] = Body(None, description="Pet object that needs to be added to the store")
+,
     token_petstore_auth: TokenModel = Security(
         get_token_petstore_auth, scopes=["write:pets", "read:pets"]
     ),

can we keep the , (comma) in the same line instead of a new line?

ref: https://github.com/OpenAPITools/openapi-generator/actions/runs/24402803325/job/71278593153?pr=23537 (please update the samples by following step 3 in the PR checklist)

Remove trailing newline from endpoint_argument_definition.mustache so
the comma after each parameter stays inline rather than appearing on a
new line.

Regenerate python-fastapi petstore sample.
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