Skip to content
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

Support patternProperties in components.schemas #754

Closed
KevinPoole opened this issue Sep 27, 2021 · 7 comments
Closed

Support patternProperties in components.schemas #754

KevinPoole opened this issue Sep 27, 2021 · 7 comments
Labels
enhancement New feature or request PRs welcome PRs are welcome to solve this issue! stale

Comments

@KevinPoole
Copy link

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:

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?

@drwpow drwpow added enhancement New feature or request PRs welcome PRs are welcome to solve this issue! labels Sep 30, 2021
@drwpow
Copy link
Contributor

drwpow commented Sep 30, 2021

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

@alexserov
Copy link

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.

@MarioHi1
Copy link

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

@drwpow
Copy link
Contributor

drwpow commented Feb 14, 2024

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!

Copy link
Contributor

github-actions bot commented Aug 6, 2024

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 github-actions bot added the stale label Aug 6, 2024
Copy link
Contributor

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 github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 15, 2024
@gzm0
Copy link
Contributor

gzm0 commented Sep 4, 2024

Related: microsoft/TypeScript#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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request PRs welcome PRs are welcome to solve this issue! stale
Projects
None yet
Development

No branches or pull requests

5 participants