Skip to content

Using json_schema_generator to generate $ref values for OpenAPI schemas.#49

Merged
drhagen merged 9 commits into
drhagen:masterfrom
agocs-certara:openapi_hardcoded_refs
Mar 21, 2026
Merged

Using json_schema_generator to generate $ref values for OpenAPI schemas.#49
drhagen merged 9 commits into
drhagen:masterfrom
agocs-certara:openapi_hardcoded_refs

Conversation

@agocs-certara

@agocs-certara agocs-certara commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

Using PyDantic's GenerateJSONSchema's get_cache_defs_ref_schema function to create references.

https://docs.pydantic.dev/latest/api/json_schema/#pydantic.json_schema.GenerateJsonSchema.get_cache_defs_ref_schema

Note:
Pydantic deduplicates same schemas by replacing them with a single definition. The x-source-module (an OpenAPI property) is used to make each class's schema unique. So, Pydantic won't deduplicate them.

@codecov-commenter

codecov-commenter commented Mar 19, 2026

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 95.65217% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.65%. Comparing base (19d3048) to head (68453fc).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/serialite/_decorators.py 50.00% 0 Missing and 2 partials ⚠️
src/serialite/_mixins.py 83.33% 1 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #49      +/-   ##
==========================================
- Coverage   92.66%   92.65%   -0.02%     
==========================================
  Files          34       34              
  Lines        1486     1511      +25     
  Branches      225      228       +3     
==========================================
+ Hits         1377     1400      +23     
  Misses         47       47              
- Partials       62       64       +2     

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

@drhagen drhagen left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This is a great addition. I think some details need to be worked out.

Comment thread src/serialite/_base.py Outdated
Comment thread src/serialite/_base.py Outdated
Comment thread src/serialite/_mixins.py Outdated
Comment thread src/serialite/_mixins.py Outdated
subclass.to_openapi_schema(json_schema_generator=json_schema_generator)
for subclass in cls.__subclass_serializers__.values()
],
"x-source-module": f"{cls.__module__}.{cls.__qualname__}",

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I am not convinced this is necessary. More evidence is required.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I figured out what this is really doing.

It is true that FastAPI deduplicates identical components. It does this when the OpenAPI schema representation is completely identical for the entire object. It does not deduplicate on the name alone. To block depulication, you need to change something/anything about the schema representation. You could do "foo": id(cls)" and get the same behavior even if the object has the same module and qualname.

FastAPIs behavior seems...fine. The generated OpenAPI gets a little simpler, but a little fragiler. If you change one class, then it splits into two in the generated SDKs. In any case, this seems like a FastAPI problem rather than a Serialite problem. Does Pydantic do something to ensure that this deduplication never triggers?

@agocs-certara agocs-certara requested a review from drhagen March 20, 2026 14:28
@drhagen drhagen merged commit 224d086 into drhagen:master Mar 21, 2026
6 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.

3 participants