Skip to content

Improve type-safety of query parameters with content field #2569

@demhadais

Description

@demhadais

Description

Hi! First of all, love this package. It's so much lighter and simpler than the alternatives.

I have an OpenAPI document with a complex query (nested objects and arrays and whatnot), so it uses content inside of a parameter definition instead of schema (irrelevant details removed):

{
  "in": "query",
  "name": "my_complex_query",
  "content": {
    "application/json": {
      "schema": {
        "type": "object",
        "properties": {
          // Some complex object schema here
        }
      }
    }
  }
}

This is allowed by the specification. Currently, openapi-typescript generates the following:

export interface paths {
    "/people": {
        get: {
            parameters: {
                query?: {
                    my_complex_query?: string; // this should be my complex object from above
                };
                header?: never;
                path?: never;
                cookie?: never;
            };
            requestBody?: never;
        };
    };
}

This is technically correct, but we lose the nice type-safety, one of the main features of this library.

Proposal

I don't know if this would open a can of worms, but it would be awesome if this paths interface could use the JSON schema from the OpenAPI document. Then, if using openapi-ts/openapi-fetch, the resulting client could just serialize the parameter using JSON.stringify.

I'm sure this would take a lot of work to get a feature-rich implementation, but I'd be happy to take a stab at a minimal one. I'm pretty bad at TypeScript so it'd take me awhile, but this would be a good way to learn.

Thanks!

Extra

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestopenapi-tsRelevant to the openapi-typescript library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions