-
Notifications
You must be signed in to change notification settings - Fork 109
Invalid interface generated by import when allOf
is used with an object type
#317
Comments
Nice catch! Thanks for the repro 👌 |
I need to check for this behavior of |
I think it's quite a difficult problem to handle merging constraints from different schemas, where the base types for the properties are defined in one schema, but the required/nullable constraints come from another. The current approach of converting interface Foo {
a?: string;
}
interface Bar {
a: any | null;
}
type Baz = Foo & Baz;
let x: Baz; // x.a is of type `any` rather than `string` | `null` I imagine you could define a mapped type that could make specific properties nullable / required ( |
I will not call this a difficult problem, we "just" need to combine the specs before computing the type. I didn't payed enought attention to this part of specs. I would also love to keep the composition when possible, because this can add some context about the type so this is always good to have (and this is how this task become a more tricky 😁) BTW, regarding this This said, I will totally enjoy this little |
I believe |
Describe the bug
The code generated by importing an OpenAPI definition that featured a
requestBody
usingallOf
to combine multiple types, including an object type, is invalid, and the fixed version of that code does not match the semantics of the OpenAPI definition.To Reproduce
I imported the following OpenAPI definition
by running
npx restful-react import --file customer-api.yaml --output customer-api.tsx.
The generated TypeScript output code included a broken interface definition:
This should probably be defined as an intersection type instead to make it valid TypeScript:
However, this type still does not behave as intended:
Segment
inApiCustomersUpdateRequestBody
cannot havenull
assigned to it.FirstName
,LastName
andDOB
would still be optional.Expected behavior
I expected the TypeScript generated by the import of the OpenAPI definition to be valid, and for the generated
ApiCustomersUpdateRequestBody
to have a nullableSegment
property, and requiredFirstName
,LastName
andDOB
properties.Desktop (please complete the following information):
Additional context
I was trying out the code from this guide, which is intended to solve the problem of lots of repetition in schemas for related endpoints, where properties can differ in terms of nullability or optionality across endpoints. The solution involves splitting schemas into reusable parts that are composed together using OpenAPI's
allOf
.The text was updated successfully, but these errors were encountered: