You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I thought about this (for a year+, lol), and I think proper solution is to move from using schemars generator as container for all schemas to custom implementation.
Approximate design: current Components removed and replaced with OpenApiBuilder in all signatures. OpenApiBuilder have following definition
structOpenApiBuilder{spec:OpenApi,// spec from okapiconflict_schemas:HashSet<String>,}
where spec's components.schemas will act as storage for all known referenceable schemas and conflict_schemas will include type names, where different types had same name.
Builder will provide single method .schema_for<T: JsonSchema>() -> RefOr<SchemaObject> with this logic:
If T is not referenceable, then return it as-is, otherwise proceed to next steps;
Generate root schema with schemars generator
For each schema in generated root schema:
If schema name in conflict_schemas, replace all references to new schema in generated schemas with actual definition;
If schema name is not in spec.content.schemas, put it there
If schema name is in spec.content.schemas and content is equal, skip this schema
If schema name is in spec.content.schemas and content is different:
put schema name in conflict_schemas;
remove existing schema from spec and replace all references to it in spec with actual definition;
replace all references to new schema in generated schemas with actual definition;
If current schema is for requested type, store it (ore reference to it) in additional variable.
Only problem left is if you use same name for different types like this
structRequest{a: crate_a::MyField,b: crate_b::MyField// actually different from crate_a::MyField}
If there is 2 or more types with same name, they will override each other, when schema generated. For example
handler1.rs
handler2.rs
will create only one definition in
#/components/schema
, and handlers from both files wil reference same schema.First potenial fix - enable inlining in https://docs.rs/schemars/latest/schemars/gen/struct.SchemaSettings.html#structfield.inline_subschemas, which will solve most problems (probably everything sxcept recursive types).
The text was updated successfully, but these errors were encountered: