Skip to content

Feat/openapi pydantic native - #86

Merged
NEFORCEO merged 16 commits into
masterfrom
feat/openapi-pydantic-native
Jul 3, 2026
Merged

Feat/openapi pydantic native#86
NEFORCEO merged 16 commits into
masterfrom
feat/openapi-pydantic-native

Conversation

@NEFORCEO

@NEFORCEO NEFORCEO commented Jul 3, 2026

Copy link
Copy Markdown
Member

🚀 Description

Rebuilds OpenAPI schema generation entirely on Pydantic's own JSON Schema
generation (model_json_schema() / TypeAdapter / models_json_schema())
instead of a hand-written type mapper, and adds several Swagger UI /
docs features on top of it.

OpenAPI generator (fasthttp/openapi/generator.py)

  • Replaced manual type→schema mapping with Pydantic-native generation:
    enums, unions/Optional, constraints, defaults, and nested models are
    now handled correctly instead of falling back to {"type": "string"}.
  • Nested/shared models are deduplicated via models_json_schema() and
    merged into components/schemas with proper $ref rewriting.
  • Field descriptions from Field(description=...) now come through
    natively; Doc(...) annotations (the project's existing docstring
    convention) are still backfilled where Pydantic has no description.
  • Query params and raw json=/data= bodies now surface their literal
    values as OpenAPI examples, so Swagger UI's "Try it out" pre-fills them.
  • Routes without explicit tags are now grouped by hostname instead of
    a single "Default" bucket.

Swagger UI / docs (fasthttp/openapi/swagger.py, fasthttp/app.py, fasthttp/openapi/urls.py)

  • New /redoc endpoint (ReDoc — read-only alternative view).
  • Proper dark theme for Swagger UI, targeting the real swagger-ui-dist
    classes (previous approach used a filter: invert() hack that
    distorted method-color badges, logos, and the JSON viewer).
  • persistAuthorization, requestSnippetsEnabled, and filter enabled
    in the Swagger UI config.
  • /request proxy now returns response timing (x-fasthttp-duration-ms),
    visible in the Swagger UI response headers panel.
  • 404 page now links to /redoc alongside /docs and /openapi.json.

Bug fix (fasthttp/routing.py)

  • Route.response_model was typed as type | None, which made Pydantic
    reject response_model=list[Model] at route-registration time — a
    pattern used throughout examples/ and docs/. Widened to Any.

Docs

  • docs/en/openapi.md, docs/ru/openapi.md, and the OpenAPI Swagger UI
    tutorial pages updated with the new /redoc endpoint and dark mode
    screenshots. README updated with the same screenshots.
  • New example: examples/openapi/pydantic_schema_example.py demonstrating
    enums, nested models, Field descriptions/examples, and error models.

🧩 Type of Change

  • feat: New feature
  • fix: Bug fix
  • docs: Documentation update
  • refactor: Code refactoring
  • ci: CI/CD changes
  • chore: Maintenance

✅ Checklist

  • Tests added or updated (tests/test_openapi.py rewritten against the
    new _SchemaCollector API; full suite passes — 564 passed)
  • Documentation updated (README, docs/en, docs/ru, tutorial pages)
  • No breaking changes (only private, unexported generator.py helpers
    were renamed/removed; public API — generate_openapi_schema,
    get_swagger_html, get_not_found_html, etc. — is unchanged aside
    from the additive redoc_url parameter)
  • Code follows project style

🔗 Related Issues

Fixes #


📸 Additional Context

New screenshots: docs/photo/swagger_dark.png, docs/photo/redoc.png.

Manually verified locally: /docs, /redoc, /openapi.json all serve
200 with examples/openapi/pydantic_schema_example.py; generated schema
inspected for enum $refs, nested model dedup, and example prefill.

@fasthttp-bot fasthttp-bot added tests dependencies Pull requests that update a dependency file feature labels Jul 3, 2026
@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

👋 Thanks for your contribution, @NEFORCEO!

Please make sure:

  • Code is clean and formatted
  • Tests are passing
  • PR description is clear

Maintainers will review it soon.

@codspeed-hq

codspeed-hq Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 40.35%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 6 improved benchmarks
✅ 6 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
test_response_req_text_from_json 213.2 µs 134.7 µs +58.25%
test_response_repr 196.4 µs 134.2 µs +46.43%
test_response_property_access 194.5 µs 135.2 µs +43.82%
test_response_json_parsing 205.6 µs 144.7 µs +42.04%
test_response_creation 201.3 µs 142.6 µs +41.19%
test_middleware_after_response 403.1 µs 352.5 µs +14.35%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing feat/openapi-pydantic-native (ce89aca) with master (6f3d441)1

Open in CodSpeed

Footnotes

  1. No successful run was found on master (de5b998) during the generation of this report, so 6f3d441 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@NEFORCEO
NEFORCEO merged commit a1220fb into master Jul 3, 2026
17 checks passed
@NEFORCEO
NEFORCEO deleted the feat/openapi-pydantic-native branch July 3, 2026 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file feature tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants