Skip to content

[BUG] [typescript-angular] queryParamObjectFormat=json does not work for arrays #7620

@thccorni

Description

@thccorni

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When using queryParamObjectFormat=json with query param of type array I would expect the request url to look something like

  • https://example.com/pizzas?sorting=[{"name": 1}, {"price": -1}]
    Instead of
  • https://example.com/pizzas?sorting={"name": 1}&sorting={"price": -1} or
  • https://example.com/pizzas?sorting={"name": 1},{"price": -1}
openapi-generator version

v5.0.0-beta2

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: 'Sample-API'
  version: '1.1.0'
paths:
  /pizzas:
    get:
      parameters:
        - name: filter
          in: query
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PizzaFilter'
        - name: sorting
          in: query
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PizzaSorting'
      responses:
        200:
          description: Delicious pizzas
          content:
            application/json:
              schema:
                type: array
                items:
                  type: string

components:
  schemas:
    PizzaFilter:
      type: object
      additionalProperties: true
    PizzaSorting:
      type: array
      items:
        type: object
        minProperties: 0
        maxProperties: 1
        additionalProperties:
          type: integer
          enum: 
            - 1
            - -1
Generation Details
Steps to reproduce
npx @openapitools/openapi-generator-cli version-manager set 5.0.0-beta2
npx @openapitools/openapi-generator-cli generate -i spec.yaml -g typescript-angular -o ./generated --additional-properties=queryParamObjectFormat=json
Related issues/PRs
Suggest a fix

I think isQueryParamObjectFormatJson should be checked here

{{#queryParams}}
{{#isListContainer}}
if ({{paramName}}) {
{{#isCollectionFormatMulti}}
{{paramName}}.forEach((element) => {
queryParameters = this.addToHttpParams(queryParameters,
<any>element, '{{baseName}}');
})
{{/isCollectionFormatMulti}}
{{^isCollectionFormatMulti}}
queryParameters = this.addToHttpParams(queryParameters,
{{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}']), '{{baseName}}');
{{/isCollectionFormatMulti}}
}
{{/isListContainer}}
{{^isListContainer}}
if ({{paramName}} !== undefined && {{paramName}} !== null) {
queryParameters = this.addToHttpParams(queryParameters,
<any>{{paramName}}, '{{baseName}}');
}
{{/isListContainer}}
{{/queryParams}}

Maybe something like this could work:

{{#queryParams}}
    {{#isListContainer}}
        if ({{paramName}}) {
        {{#isQueryParamObjectFormatJson}}
            queryParameters = this.addToHttpParams(queryParameters,
              <any>{{paramName}}, '{{baseName}}');
        {{/isQueryParamObjectFormatJson}}
        {{^isQueryParamObjectFormatJson}}
            {{#isCollectionFormatMulti}}
            {{paramName}}.forEach((element) => {
                queryParameters = this.addToHttpParams(queryParameters,
                  <any>element, '{{baseName}}');
            })
            {{/isCollectionFormatMulti}}
            {{^isCollectionFormatMulti}}
            queryParameters = this.addToHttpParams(queryParameters,
                {{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}']), '{{baseName}}');
            {{/isCollectionFormatMulti}}
        {{/isQueryParamObjectFormatJson}}
        }
    {{/isListContainer}}
    {{^isListContainer}}
        if ({{paramName}} !== undefined && {{paramName}} !== null) {
          queryParameters = this.addToHttpParams(queryParameters,
            <any>{{paramName}}, '{{baseName}}');
        }
    {{/isListContainer}}
{{/queryParams}}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions