-
Notifications
You must be signed in to change notification settings - Fork 403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allOf, oneOf dont correctly generate types #381
Comments
Similar to #378 ? Used version 10.1.4 |
@italicbold the mentioned issue is related to the use of allOf, anyOf with properties, and in that case there was an error in the provided example. Here the problem is related to the processing of |
I'm seeing the same problem. Usage of |
I found that nesting Minimum example schema: {
"oneOf": [ // or "allOf" or "anyOf"
{
"properties": {
"speed": { "type": "string" }
},
"oneOf": [ // or "allOf" or "anyOf"
{ "required": ["speed"] }
]
}
]
} Expected vs. actual outputExpected output: export type Demo = {
speed?: string;
[k: string]: unknown;
}; Actual output: export type Demo = {
[k: string]: unknown;
}; Workaround that yields the expected output {
"oneOf": [ // or "allOf" or "anyOf"
{
"properties": {
"speed": { "type": "string" }
},
+ "if": true,
+ "then": {
"oneOf": [ // or "allOf" or "anyOf"
{ "required": ["speed"] }
]
+ }
}
]
} Example schema with a bit more context{
"oneOf": [
{
"properties": {
"type": { "const": "NoFunction" }
},
"required": ["type"],
"additionalProperties": false
},
{
"properties": {
"type": { "const": "ShutterStrobe" },
"speed": { "type": "string" },
"speedStart": { "type": "string" },
"speedEnd": { "type": "string" }
},
"required": ["type"],
"oneOf": [
{ "required": ["speed"] },
{ "required": ["speedStart"] }
],
"dependencies": {
"speedStart": ["speedEnd"],
"speedEnd": ["speedStart"]
},
"additionalProperties": false
}
]
} When removing the inner |
AFAICT, it isn't valid JSON schema to use
(Source) So there doesn't seem to be a problem with JSON SchemaView{
"title": "Test",
"type": "object",
"allOf": [
{
"type": "object",
"properties": {
"prop1": { "type": "string" },
"prop2": { "type": "string" },
"prop3": { "type": "string" },
"prop4": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1
},
"prop5": { "type": "string" },
"prop6": { "type": "string" }
},
"additionalProperties": false
},
{
"type": "object",
"properties": { "prop1": { "type": "string" } },
"required": ["prop1"],
"additionalProperties": false
},
{
"oneOf": [
{
"type": "object",
"properties": { "prop2": { "type": "string" } },
"required": ["prop2"],
"additionalProperties": false
},
{
"type": "object",
"properties": { "prop3": { "type": "string" } },
"required": ["prop3"],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"prop4": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1
}
},
"required": ["prop4"],
"additionalProperties": false
},
{
"type": "object",
"properties": { "prop5": { "type": "string" } },
"required": ["prop5"],
"additionalProperties": false
},
{
"type": "object",
"properties": { "prop6": { "type": "string" } },
"required": ["prop6"],
"additionalProperties": false
}
]
}
]
} Outputexport type Test = {
prop1?: string;
prop2?: string;
prop3?: string;
prop4?: [string, ...string[]];
prop5?: string;
prop6?: string;
} & {
prop1: string;
} & (
| {
prop2: string;
}
| {
prop3: string;
}
| {
prop4: [string, ...string[]];
}
| {
prop5: string;
}
| {
prop6: string;
}
); |
Reading this and this, I would argue, that |
Any news on this one? Is someone working on it? |
We have encountered a similar issue, using release {
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema",
"properties": {
"things": {
"type": "array",
"minLength": 1,
"description": "A list of things",
"items": {
"type": "object",
"required": ["thingProp1", "thingProp2"],
"oneOf": [
{
"type": "object",
"properties": {
"propOption1": {
"type": "string"
}
},
"required": ["propOption1"]
},
{
"type": "object",
"properties": {
"propOption2": {
"type": "number"
}
},
"required": ["propOption2"]
}
],
"properties": {
"thingProp1": {
"type": "string"
},
"thingProp2": {
"type": "string"
}
}
}
}
}
}
Output: export interface Demo {
/**
* A list of things
*/
things?: (
| {
propOption1: string;
[k: string]: unknown;
}
| {
propOption2: number;
[k: string]: unknown;
}
)[];
[k: string]: unknown;
} It completely ignores {
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema",
"required": ["thingProp1", "thingProp2"],
"oneOf": [
{
"type": "object",
"properties": {
"propOption1": {
"type": "string"
}
},
"required": ["propOption1"]
},
{
"type": "object",
"properties": {
"propOption2": {
"type": "number"
}
},
"required": ["propOption2"]
}
],
"properties": {
"thingProp1": {
"type": "string"
},
"thingProp2": {
"type": "string"
}
}
} In this case, the generated output is as expected: export type Demo = {
thingProp1: string;
thingProp2: string;
[k: string]: unknown;
} & (
| {
propOption1: string;
[k: string]: unknown;
}
| {
propOption2: number;
[k: string]: unknown;
}
); I'll try to take a look at this issue myself, so any pointers where to start looking are very welcome. |
The following schema:
Generates type:
Expected type:
The text was updated successfully, but these errors were encountered: