json-schema-org / vocab-idl

Help and clarify how JSON Schema can be interpreted from validation rules to data definition. This extends to how those data definitions can be represented in any programming language
19 stars 4 forks source link

Handling subschemas #51

Open gregsdennis opened 1 year ago

gregsdennis commented 1 year ago

Subschemas and nested types appear everywhere. Generally, a subschema will represent a new type.

{
  "type": "object",
  "properties": {
    "foo": { "type": "integer" },
    "bar": {
      "type": "object",
      "properties": {
        "numbers": {
          "type": "array",
          "items": { "type": "number" }
        }
      }
    }
  }
}

For this schema, we have four types being represented:

(#45 discusses built-in types, so we'll leave that out of this issue.)

Specifically, we need to focus on the two custom objects: the top-level and whatever bar is.

I would expect a generator to create two types from this schema. Are there any restrictions that people can see?


What happens when a subschema is duplicated?

{
  "type": "object",
  "properties": {
    "foo": { "type": "integer" },
    "bar": {
      "type": "object",
      "properties": {
        "numbers": {
          "type": "array",
          "items": { "type": "number" }
        }
      }
    },
    "baz": {
      "type": "object",
      "properties": {
        "numbers": {
          "type": "array",
          "items": { "type": "number" }
        }
      }
    }
  }
}

Here, /properties/bar and /properties/baz are identical. Is this the author's intent, or are these two semantically (business rules) different yet functionally (shape/structure) identical? Do we need a way to discern this? Maybe #50 can help identify author intent. For example, if the subschemas have the same title, it was intended that they're the same type.

($ref'd subschemas are obviously the same type.)

spacether commented 1 year ago

Json schema type integer allows integer and float types with integer values. Should code generation represent that as integer only? If so then that is a departure form json schema definitions.

gregsdennis commented 1 year ago

I think that depends on the lanuage being generated.

Typescript doesn't have an integer type, so it won't care.

C# has both, and the serializer will actually decode 1.0 as an integer.

I don't think we can specify how individual languages support things. We can only generalize.