Skip to content

fix: change Optional to NotRequired in OpenAPI pydantic plugin#4347

Merged
provinzkraut merged 2 commits intolitestar-org:mainfrom
raidzin:fix
Oct 3, 2025
Merged

fix: change Optional to NotRequired in OpenAPI pydantic plugin#4347
provinzkraut merged 2 commits intolitestar-org:mainfrom
raidzin:fix

Conversation

@raidzin
Copy link
Copy Markdown
Contributor

@raidzin raidzin commented Sep 29, 2025

Description

Change wrapper from Optional to NotRequired for pydantic fields with default_factory

Now fields appear as string and not required instead of (string | null)

Closes

Fixes #4294

@raidzin raidzin force-pushed the fix branch 3 times, most recently from 051356e to 7e6a43d Compare September 30, 2025 07:02
@raidzin raidzin marked this pull request as ready for review September 30, 2025 07:14
@raidzin raidzin requested review from a team as code owners September 30, 2025 07:14
@raidzin
Copy link
Copy Markdown
Contributor Author

raidzin commented Sep 30, 2025

@provinzkraut, can i remove this test as not actual?

def test_v1_constrained_str_with_default_factory_does_not_generate_title() -> None:
# https://github.com/litestar-org/litestar/issues/3710
class Model(pydantic_v1.BaseModel):
test_str: str = pydantic_v1.Field(default_factory=str, max_length=600)
@post(path="/")
async def test(data: Model) -> str:
return "success"
schema = Litestar(route_handlers=[test]).openapi_schema.to_schema()
assert (
"title"
not in schema["components"]["schemas"][
"test_v1_constrained_str_with_default_factory_does_not_generate_title.Model"
]["properties"]["test_str"]["oneOf"][1]
)

@provinzkraut
Copy link
Copy Markdown
Member

@provinzkraut, can i remove this test as not actual?

This is a regression test for the linked issue. Since we're still supporting Pydantic v1, that test should not break so no, it cannot be removed

@raidzin raidzin force-pushed the fix branch 2 times, most recently from 46a9365 to 0e3f33f Compare September 30, 2025 09:34
@codecov
Copy link
Copy Markdown

codecov bot commented Sep 30, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.93%. Comparing base (7c9d0e0) to head (872c88e).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4347   +/-   ##
=======================================
  Coverage   97.93%   97.93%           
=======================================
  Files         319      319           
  Lines       15585    15586    +1     
  Branches     1726     1726           
=======================================
+ Hits        15263    15264    +1     
  Misses        184      184           
  Partials      138      138           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@provinzkraut provinzkraut added the cherry-pick v2 Automatically create a cherry-pick PR against the `v2` branch when this Pull Request is merged label Sep 30, 2025
@provinzkraut
Copy link
Copy Markdown
Member

@raidzin Can you add a changelog entry?

@github-actions github-actions bot added the area/docs This PR involves changes to the documentation label Sep 30, 2025
@raidzin
Copy link
Copy Markdown
Contributor Author

raidzin commented Sep 30, 2025

@raidzin Can you add a changelog entry?

Done

@raidzin raidzin force-pushed the fix branch 2 times, most recently from 375a85e to f54445a Compare September 30, 2025 14:33
@github-actions
Copy link
Copy Markdown

github-actions bot commented Oct 3, 2025

Documentation preview will be available shortly at https://litestar-org.github.io/litestar-docs-preview/4347

@provinzkraut provinzkraut merged commit ce3ff9e into litestar-org:main Oct 3, 2025
28 checks passed
provinzkraut pushed a commit that referenced this pull request Oct 5, 2025
fix: openAPI Schema generator incorrect type for fields with default_factory

(cherry picked from commit ce3ff9e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/docs This PR involves changes to the documentation cherry-pick v2 Automatically create a cherry-pick PR against the `v2` branch when this Pull Request is merged pr/external pr/internal size: small Triage Required 🏥 This requires triage type/bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: OpenAPI Schema generator incorrect type for fields with default_factory

2 participants