-
Notifications
You must be signed in to change notification settings - Fork 408
Case where allOf is not included in compiled ts interface definition #47
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
Comments
I haven't tested, but a similar test case for |
What is expected output? Is it 1 export interface Foo {
a: string;
b: number;
}
export interface Bar {
a: string;
}
export interface AllOf {
fooAndBar: Foo & Bar;
foo: Foo;
allOf: {
more?: Bar;
};
} Or 2 export interface Foo {
a: string;
b: number;
}
export interface Bar {
a: string;
}
export interface AllOf {
fooAndBar: Foo & Bar;
foo: Foo;
} Or something else? |
I don't consider 2 a candidate. Your candidate 1 is close I think. Case 3 (is another which I mentioned in first commit... but I know this is wrong) export interface Foo {
a: string;
b: number;
}
export interface Bar {
a: string;
}
export interface AllOf {
fooAndBar: Foo & Bar;
foo: Foo;
more: Bar;
} Note: 3 agrees with However, I played with: http://json-schema-validator.herokuapp.com/index.jsp and discovered some differences in my expectations and what the schema actually defined. In particular: Revised schema after playing with validating schema and the JSON: {
"title": "AllOf",
"type": "object",
"properties": {
"fooAndBar": {
"type": "object",
"allOf": [
{"$ref": "#/definitions/foo"},
{"$ref": "#/definitions/bar"}
]
},
"foo": {
"type": "object",
"allOf": [
{"$ref": "#/definitions/foo"}
]
}
},
"allOf": [{
"title": "AnotherAllOf",
"type": "object",
"additionalProperties": true,
"properties": {
"more": {
"type": "object",
"allOf": [
{"$ref": "#/definitions/bar"}
]
}
}
}],
"definitions": {
"foo": {
"properties": {
"a": { "type": "string" },
"b": { "type": "integer" }
},
"additionalProperties": true,
"required": ["a", "b"]
},
"bar": {
"properties": {
"a": { "type": "string" }
},
"additionalProperties": true,
"required": ["a", "b"]
}
},
"required": ["fooAndBar", "foo", "AnotherAllOf"],
"additionalProperties": true
} Example JSON that validates: {
"fooAndBar": {
"a": "something",
"b": 1
},
"foo": {
"a": "something",
"b": 1
},
"AnotherAllOf":{
"more": {
"b": 1
}
}
} And based on this valid JSON, I would say this is expected typescript interface: Case 4: export interface Foo {
a: string;
b: number;
}
export interface Bar {
a: string;
}
export interface AllOf {
fooAndBar: Foo & Bar;
foo: Foo;
AnotherAllOf: {
more?: Bar;
};
} Latest is here: https://github.com/darcyparker/json-schema-to-typescript/blob/1591ff0d7d34741c3bf2996ef672ef2aa829e03f/test/cases/allOf.ts |
I put it there because
This does not seem to be right - it's flattening In general, let's try to refer to the JSON-schema spec, official examples, and official test suite. I don't trust most of these community implementations. |
I agree 3 is wrong after playing with things. 2 is close to correct. And after I modified the schema, case 4 is probably close to correct. (But I am not confident what the right answer is yet.) I agree going through the official examples you mentioned is the next step and derive expected typescript interface definitions that should be expected. Side note: (I have lots of these cases of |
Do you want help with #1 ? Here's how I am thinking I could help:
|
Notice |
Interesting - so it looks like an alternative syntax. Ie. "allOf": [
{
"properties": {
"bar": {"type": "integer"}
},
"required": ["bar"]
},
{
"properties": {
"foo": {"type": "string"}
},
"required": ["foo"]
}
] Is equivalent to "properties": {
"bar": {"type": "integer"},
"foo": {"type": "string"}
},
"required": ["bar", "foo"] And both should compile to interface allOf {
bar: number;
foo: string;
} |
If you want, feel free to port the official test cases to a branch in this repo. I added you as a collaborator. |
Yes, it looks like an alternate syntax. It seems like it is popular pattern in the JSON Schema I have been receiving. The JSON I am receiving is serialized from C# classes... and it looks like the inherited structure of properties are grouped using Thanks for the invite to be a collaborator. I am happy to take this on. (I have enjoyed studying the code and watching it iterate the past few weeks. And I think its going to be a valuable tool. I am quite excited about the potential of having these interfaces built automatically for me. And I think others will appreciate it too.) |
Good to hear! I'm working on #44 today and tomorrow; specifically-
I'll also be refactoring the code a bit. If you can stick to adding test cases till this is done to avoid merge conflicts, it would be much appreciated. Will keep you posted. |
NP - I will stick to adding the test cases. I won't get to it over the weekend, but will will start early this week. |
Cool - again, thanks for all the help! |
Fixed in v2 - please reopen if you have any issues. |
Have a look at: https://github.com/darcyparker/json-schema-to-typescript/blob/fe3f08f1d7eb4445619488974f9f89b2353b74c7/test/cases/allOf.ts
As a comparison, dtsgenerator creates this output:
The text was updated successfully, but these errors were encountered: