openapi-typescript icon indicating copy to clipboard operation
openapi-typescript copied to clipboard

Support `patternProperties` in `components.schemas`

Open KevinPoole opened this issue 4 years ago • 4 comments

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?

KevinPoole avatar Sep 27 '21 23:09 KevinPoole

Interesting. What would be the desired TypeScript generated types for this?

drwpow avatar Sep 30 '21 20:09 drwpow

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.

alexserov avatar Aug 02 '22 06:08 alexserov

Any update on this? Without this spec, it is not truly 3.1 compatible.

MarioHi1 avatar Feb 14 '24 09:02 MarioHi1

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!

drwpow avatar Feb 14 '24 13:02 drwpow

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.

github-actions[bot] avatar Aug 06 '24 12:08 github-actions[bot]

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.

github-actions[bot] avatar Aug 15 '24 02:08 github-actions[bot]

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).

gzm0 avatar Sep 04 '24 09:09 gzm0