oxidecomputer / typify

compiler from JSON Schema into idiomatic Rust types
Apache License 2.0
384 stars 54 forks source link

consider type naming #1

Open ahl opened 2 years ago

ahl commented 2 years ago

JSON schema gives us two direct sources of types names: the keys in references dictionary and the title field in schema metadata. Note also that while the former are necessarily unique, the latter may not be. In addition, we can get hints about names. Consider for example a JSON object that looks like this:

{
    "fooObj": {
        "a": 7,
        "b": 8
    }
}

The outer object may have a name, but the value of fooObj may not (or it may have a title that is not unique). We could include a "distinguishing hint" in these cases which might be the enclosing type and property name, or may come from the external caller e.g. our OpenAPI processor could use the operationId.

After processing all types, we could choose to always include the distinguishing hint in the name, only include it if it was needed to resolve conflicting types, or raise an error if it as needed to disambiguate conflicting types.

This would require a change in the API which currently is of the form schemars::schema::Schema -> proc_macro2::TokenStream i.e. we will no longer be sure of the type name until all schemas have been examined.

untitaker commented 1 year ago

"title" can appear at any level of the schema too, and that is actually very convenient because it means one can define type structure separately from schema structure.

I would prefer an implementation that looks for title first and for definition name second. It just so happens to line up with schema generator jsonschema-gentypes in Python: https://github.com/camptocamp/jsonschema-gentypes