Skip to content

fix: sort openapi type schema#4239

Merged
provinzkraut merged 2 commits intolitestar-org:mainfrom
tjhoff:main
Jul 25, 2025
Merged

fix: sort openapi type schema#4239
provinzkraut merged 2 commits intolitestar-org:mainfrom
tjhoff:main

Conversation

@tjhoff
Copy link
Copy Markdown

@tjhoff tjhoff commented Jul 16, 2025

Description

  • Sorts orders of types when using Literal[*] | None

Consider the following OpenAPI schema type:
{'type': ['null', 'string'], 'enum': ['1', None]}

Currently, there are scenarios (with environmental differences) in which this can instead be rendered as:
{'type': ['string', 'null'], 'enum': ['1', None]}

This can cause issues with continuous integration in monorepos where generated openapi json is checked into the source tree and validated for changes.

Another discussion that this might spawn is why this is not in the OAPI 3.0 oneOf format instead -
{'oneOf': [{'type': 'string', 'const': '1'}, {'type': 'null'}]}

This appears to be consistently sorted by something else in Litestar's openapi generation.

Fixes #3646

@tjhoff tjhoff requested review from a team as code owners July 16, 2025 17:24
@github-actions github-actions bot added area/docs This PR involves changes to the documentation area/private-api This PR involves changes to the privatized API size: small type/bug pr/external Triage Required 🏥 This requires triage labels Jul 16, 2025
@codecov
Copy link
Copy Markdown

codecov bot commented Jul 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.38%. Comparing base (1daf8ef) to head (e5efe63).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4239   +/-   ##
=======================================
  Coverage   98.38%   98.38%           
=======================================
  Files         348      348           
  Lines       15895    15895           
  Branches     1756     1756           
=======================================
  Hits        15638    15638           
  Misses        121      121           
  Partials      136      136           

☔ 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.

@tjhoff
Copy link
Copy Markdown
Author

tjhoff commented Jul 24, 2025

@provinzkraut any chance you could take a look at this as you responded to the initial issue? Sorry for the tag!

Copy link
Copy Markdown
Member

@provinzkraut provinzkraut left a comment

Choose a reason for hiding this comment

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

Looks good. I like the idea you suggested of using oneOf instead though. Would you want to give implementing that a try as well?

@provinzkraut provinzkraut enabled auto-merge (squash) July 25, 2025 13:41
@provinzkraut provinzkraut merged commit c33085a into litestar-org:main Jul 25, 2025
26 checks passed
@github-actions
Copy link
Copy Markdown

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

@tjhoff
Copy link
Copy Markdown
Author

tjhoff commented Jul 25, 2025

Thanks! I'll take a look and see if it's straightforward.

@tjhoff
Copy link
Copy Markdown
Author

tjhoff commented Jul 25, 2025

After some digging, this seems like it was explicit behavior added here. I'm not quite clear on what the ramifications of changing this back would be; however, in my testing I did note that Literal | <anything> | null results in a oneOf type.

I might leave this alone to prevent causing a regression to someone else! 😅

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 area/private-api This PR involves changes to the privatized API 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: order of types in openapi spec is not consistent in json rendering

2 participants