Support `patternProperties` in `components.schemas`
I have a component.schema of the following form:
"SomeSchemaName": {
"title": "SomeSchemaName",
"type": "object",
"properties": {
"somePropertyName": {
"type": "object",
"additionalProperties": false,
"patternProperties": {
"^[0-9]+$": {
"$ref": "#/components/schemas/OtherSchemaName"
}
}
}
}
},
Of particular importance is the fact that I am utilizing patternProperties. This is a valid usage according to the following issue:
- https://github.com/OAI/OpenAPI-Specification/issues/687
Anyone know if this is something that would be expected to work right now? If not, any ideas on what it would take to support this?
Interesting. What would be the desired TypeScript generated types for this?
Interesting. What would be the desired TypeScript generated types for this?
Having an interface that allows fields with arbitrary keys but specifies types would be extremely helpful.
For example, we have the following schema: MobilityData/gbfs-json-schema/gbfs.json.
Current version of the openapi-typescript produces the following output:
interface GbfsV2_3 {
//...
data: { [key: string]: unknown };
};
Instead of unknown we could have the following:
interface GbfsV2_3 {
//...
data: {
[key: string]: {
//...
feeds?: {
//...
};
};
};
};
This will allow us extract the type of the data field like follows:
type DataType = GbfsV2_3['data'];
type DataFieldType = DataType[keyof DataType];
If there are multiple patterns in the schema, openapi-typescript can generate type unions. Not too strict, but better than unknown.
Any update on this? Without this spec, it is not truly 3.1 compatible.
I’d love to support patternProperties, but I’m not sure this would work as intended in static TypeScript types. Mixing static object keys with generic signatures like [key: string] already leads to some rough edges, but also TS isn’t capable of truly implementing RegEx without some runtime component.
I’m not saying it’s necessarily impossible; I’m just saying I’d love a PoC that shows how this could be handled in TS and would welcome PRs adding this!
This issue is stale because it has been open for 90 days with no activity. If there is no activity in the next 7 days, the issue will be closed.
This issue was closed because it has been inactive for 7 days since being marked as stale. Please open a new issue if you believe you are encountering a related problem.
Related: https://github.com/microsoft/TypeScript/issues/41160
@drwpow I might take a stab at this. My idea is to completely ignore regexes and simply take the union type of all patternProperties on the right hand side of the index type. This gives us typing which is "as good as TS allows us to". This is in line with other things (e.g. the pattern property on string types).