Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #7620
Closed
Open
Issue created Oct 07, 2020 by Administrator@rootContributor4 of 6 checklist items completed4/6 checklist items

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

Created by: 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 https://github.com/OpenAPITools/openapi-generator/blob/d8ba49b267d7bfe9d558631d2888a6d54a1c3bf8/modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache#L177-L198

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}}
Assignee
Assign to
Time tracking