-
Notifications
You must be signed in to change notification settings - Fork 38
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
Allow usage for indexed types on body elements #1034
Comments
@lfportal Happy to work on it, and get any implementation ideas you may have on the same. Any motivation on why it was initially not permitted would be beneficial! |
I also noticed the following which capability matrix: Would love to hear any thoughts you have on implementing these. |
This was considered, but was not a priority use case for us. Ultimately we deemed TypeScript's indexed types not flexible enough to support this feature in a sufficiently generic way. Where an indexed type is present, defined sibling properties must also be assignable to the index type definition: interface CreateUserResponse {
firstName: string; // Property 'firstName' of type 'string' is not assignable to string index type 'boolean'.
[key: string]: boolean;
} |
The type parser would need to be modified to pass around some sort "type parse stack" structure to keep track of types currently being parsed as it traverses the types to short circuit reference type parsing. I'm not too familiar with recursive type representation with the OpenAPI spec. Are you aware of any particular limitations @mahirk?
interface Generic<T> {
genericProp: T;
}
type A = Generic<String> Off the top of my head:
Questions that need answering:
Thoughts @mahirk? |
Apologies, @lfportal got pulled off to something else:
Absolutely, I am curious though, what about scenarios which could enable an open-ended pair of single level JSONs? Although I do understand the type safety aspect of it, I was thinking more along the lines of the metadata which is available here: https://stripe.com/docs/api/metadata. You can see the definition here: https://raw.githubusercontent.com/stripe/openapi/master/openapi/spec3.yaml
Swagger UI seems to have trouble, and potentially the model generation might have issues. Let me check those and then evaluate if it would be okay. As far as representation, that's possible using the specification swagger-api/swagger-ui#3325
I agree with that, it may be something the system calls out to the developers. I don't forsee any issues with generating the type in that way however. |
Do you mean to allow index signatures only where no sibling properties are defined? i.e: // allowed
interface A {
[key: string]: string;
}
// not allowed
interface B {
[key: string]: string;
a: string
} |
Yup @lfportal i.e. if it has sibling properties, then spot can log an error stating that sibling properties on index types are not permitted. That should satisfy the use case and maybe also force better design of open-ended structs? |
I'm thinking a simpler way to constrain the usage could be TypeScript's |
Catching up with the discussion.
Just to confirm we will store Generic in the
inline type works great when the Generic object is small, but when its big and frequently referenced, the cost of expanding it every where goes up very quickly. The name clashes should not be a problem if we just use the full name Also, are we only considering handle custom generic type, or we will also support typescript utility types? https://www.typescriptlang.org/docs/handbook/utility-types.html As a side note on typescript supported types, I noticed:
|
The type table is used to generate the body: {
a: Generic<String>; // inline type, rendered as an inline schema in OpenAPI
b: MyType; // reference type, rendered as a $ref schema in OpenAPI
}
type MyType = Generic<String> // stored in type table
This is currently expected. However,
Yup, agreed, I'll leave this issue to focus on indexed types. |
This feature would be really useful for us as well. Allowing indexed types without explicitly listed siblings is more than enough. |
Is your feature request related to a problem? Please describe.
The JSON specification allows an indexed type by defining it as an object which can have any number of strings in it, for example:
the following type definition:
Becomes:
Describe the solution you'd like
The following spec fails:
with the error as
indexed types are not allowed
.The proposal would be to update the type-parser to allow indexed types and define them using the guidelines provided above.
The text was updated successfully, but these errors were encountered: