kstasik / schema-tools

Set of tools for codegen, preprocessing and validation of json-schema and openapi spec
MIT License
41 stars 13 forks source link

dereference output depends on order of input schemas #67

Open jayvdb opened 1 year ago

jayvdb commented 1 year ago

We have a bunch of schemas being included from external files.

If a schema refers to a schema that appears after it, like this, the result is very different ...

    SchemaDUsingC:
      $ref: "./schemas/SchemaDUsingC.yaml#/components/schemas/SchemaDUsingC"
    SchemaA:
      $ref: "./schemas/SchemaA.yaml#/components/schemas/SchemaA"
    SchemaBUsingA:
      $ref: "./schemas/SchemaBUsingA.yaml#/components/schemas/SchemaBUsingA"
    SchemaCUsingB:
      $ref: "./schemas/SchemaCUsingB.yaml#/components/schemas/SchemaCUsingB"

... to when the schemas are ordered so they always only include prior schemas, like:

    SchemaA:
      $ref: "./schemas/SchemaA.yaml#/components/schemas/SchemaA"
    SchemaBUsingA:
      $ref: "./schemas/SchemaBUsingA.yaml#/components/schemas/SchemaBUsingA"
    SchemaCUsingB:
      $ref: "./schemas/SchemaCUsingB.yaml#/components/schemas/SchemaCUsingB"
    SchemaDUsingC:
      $ref: "./schemas/SchemaDUsingC.yaml#/components/schemas/SchemaDUsingC"

I can provide more details of the schemas if that helps, and reduce it to a few schemas only to better demonstrate the problem. We're able to workaround it by carefully ordering the schemas.

kstasik commented 1 year ago

You are right. Currently you have to provide all schemas in OpenApi document starting from the most simple: https://github.com/kstasik/schema-tools-templates/blob/master/tests/rust/spec/api/api.yaml#L16

I was thinking about rebuilding it. Instead of generating names on demand during recursive iteration program would have to gather flat structure of entities with suggested names and relations (for example parent schema reference). Sort them by putting simple structures at the top and then starting the naming process.