fabien0102 / openapi-codegen

A tool for generating code base on an OpenAPI schema.
499 stars 62 forks source link

Correct handling of `style` and `explode`. #238

Open r34son opened 6 months ago

r34son commented 6 months ago

OpenAPI spec:

targets:

  get:
    tags:
      - targets
    operationId: getProductTargets
    parameters:
      - name: searchParameters
        description: "Some description"
        in: query
        required: true
        schema:
          $ref: "..ProductTargetsSearch"
    responses:
      200:
        description: "Some description"
        content:
          application/json:
            schema:
              $ref: "..GetProductTargetsResponse"
      400:
        description: "Some description"
        content:
          application/json:
            schema:
              $ref: "..ErrorDto"
      500:
        description: "Ошибка на сервере"
        content:
          application/json:
            schema:
              $ref: "..ErrorDto"

Generator emits type for queryParams:

export type GetProductTargetsQueryParams = {
    /**
     * Some description
     */
    searchParameters: Schemas.ProductTargetsSearch;
};

But by spec https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#parameter-object parameter in: query has default value of style - form. So explode default value is true for form style. Spec says:

When this is true, parameter values of type array or object generate separate parameters for each value of the array or key-value pair of the map

Should generator emit queryParam type with spreaded params? if no, why?

r34son commented 6 months ago

@fabien0102 Hi Fabien! Thanks for the great work! Could you take a look at that?

fabien0102 commented 6 months ago

Hey, thx for raising the issue, I don't think the style and explode are implemented here.

Usually, the entire API should follow the same style so you can handle this in your custom fetcher. This is at least the assumption I did ^^ I'm always happy to improve the library to handle more usecase, so if you have time, I'm open to pull-request 😀