Using json_schema_generator to generate $ref values for OpenAPI schemas.#49
Conversation
|
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
drhagen
left a comment
There was a problem hiding this comment.
This is a great addition. I think some details need to be worked out.
| 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__}", |
There was a problem hiding this comment.
I am not convinced this is necessary. More evidence is required.
There was a problem hiding this comment.
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?
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.