Marwes / schemafy

Crate for generating rust types from a json schema
MIT License
242 stars 51 forks source link

generate: incomplete types due to nested definitions #2

Closed lucab closed 7 years ago

lucab commented 7 years ago

It looks like the library is producing incomplete code due to missing types in case of nested definitions.

As a short example, let's take the following JSON schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "nestedtest",
  "type": "object",
  "properties": {
  },
  "required": [
    "top"
  ],
  "definitions": {
    "top": {
      "type": "object",
      "properties": {
        "basic": {
          "type": "string"
        },
        "nested": {
          "$ref": "#/definitions/top/definitions/defnested"
        }
      },
      "definitions": {
        "defnested": {
          "type": "object",
          "properties": {
            "append": {
              "type": "string"
            }
          }
        }
      }
    }
  }
}

which produces the following struct:

# [ serde ( rename = "top" ) ]
# [ derive ( Clone , PartialEq , Debug , Default , Deserialize , Serialize ) ]
pub struct Top {
    pub basic: Option<String>,
    pub nested: Option<Defnested>,
}

However, the library is not generating any corresponding definition for Defnested.

Marwes commented 7 years ago

I don't think I needed that features so it seems I missed that case :) Shouldn't be to difficult to fix though.

mre commented 7 years ago

In the same vein, could we also support external refs?

"nested": {
   "$ref": "defnested.json"
}

This would greatly help me with my current project.

Marwes commented 7 years ago

@lucab Released version 0.2.2 which handles nested definitions.

@mre Sure, though that is unfortunately not trivial to add due to some unfortunate implementation details. I may try to do a refactor later this week.

lucab commented 7 years ago

@Marwes thanks for the fast reaction, it looks fine from a very quick test! I'll report separately if I hit other corner-cases.

@mre do you mind moving the external-refs issue to its own ticket? This one can otherwise be closed from the point of view of the original issue.

mre commented 7 years ago

Absolutely. Moved to new issue. 👍

lucab commented 7 years ago

Nested-defs support landed, external-refs tracked at https://github.com/Marwes/schemafy/issues/3. Closing this.