quicktype
quicktype copied to clipboard
allOf in schema is not merged in Typescript output
Can someone help please:
I am using quicktype to generate typescript types from json schema. It appears that allOf is not recognised. For example, output of below creates an interface without any attributes from AmountAndFrequency. How can I resolve it?
"Income": { "allOf": [ { "$ref": "#/definitions/AmountAndFrequency" }, { "type": "object", "properties": { "type": { "type": "string", "enum": [ "GrossSalaryAmount", "Overtime" ] }, "description": { "type": "string" } }, "additionalProperties": false, "required": [ "type" ] } ] },
Thanks
Not sure if you have already resolved this, but it seems that you need to set "additionalProperties": true on both the schema you are extending, and the schema where you are implementing the extended properties.
Example:
{
"$id": "person.base.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "person-base",
"type": "object",
"properties": {
"name": {
"title": "name",
"type": "string"
}
},
"required": ["name"],
**"additionalProperties": true**
}
Person Extends Person-Base
{
"$id": "person.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "person",
"type": "object",
"allOf": [{ "$ref": "person.base.json" } ],
"properties": {
"age": {
"title": "age",
"type": "integer"
}
},
**"additionalProperties": true**
}
Doing this in my personal code seems to work. I am not a huge fan of setting additional properties to true, as I don't really want additional properties...
I might alter my generation script to set additionalProperties to true only for the generation so that the typescript gets generated correctly for the time being. If you do the same, I suggest you create a unit test so that you can verify that if they ever change the way the typescript is generated with additional properties you will know.
This is not yet fixed I encountered the same issue Today, I fixed it by removing additionalProperties to both schemas, but I don't like that idea, anyway for now It's not a big problem.
It would be good that the generated type is constructed like:
type BasePerson = {...}
type ExtendedPersion = BasePerson & {...}
instead of copying all properties from base type.
Bump on this as it's also an issue for me