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

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

Closed
4 of 6 tasks
thccorni opened this issue Oct 7, 2020 · 1 comment · Fixed by #7649
Closed
4 of 6 tasks

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

thccorni opened this issue Oct 7, 2020 · 1 comment · Fixed by #7649

Comments

@thccorni
Copy link

thccorni commented Oct 7, 2020

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}}
@GrizliK1988
Copy link
Contributor

Hi!

I'd like to take care of this one.

GrizliK1988 added a commit to GrizliK1988/openapi-generator that referenced this issue Oct 11, 2020
macjohnny pushed a commit that referenced this issue Oct 13, 2020
jimschubert added a commit that referenced this issue Oct 17, 2020
* master: (66 commits)
  [Typescript][Angular] Fix generated README when using apiModulePrefix (#7725)
  remove outdated scala files (#7723)
  [FEAT][TYPESCRIPT-ANGULAR] Add configurationPrefix option to allow generating unique configuration token (#7731)
  [bug] Fix FILES sort and path provider issue (#7729)
  better csharp tests (#7727)
  [go] Improve examples generation (#7576)
  Fixes #7635: typescript-inversify generator wrongly handles array type parameters (#7636)
  [Java] Fix import mapping for arrays with reference items of type string (#7182)
  [Java][Native] Support oneOf/anyOf schemas (#7263)
  [BUG][Ada] Incorrect client Ada code generated (#7719)
  add cake, sbt integration (#7713)
  Use 3.0 spec in documentations, update docs  (#7710)
  remove github.com/antihax/optional from go.sum (#7692)
  Update junit to newer version (4.13.1) (#7690)
  [Fix/Dart2] Resolve an exception with status 204 and no body. (#7647)
  [typescript-angular] pass array as a single JSON string to url query when queryParamObjectFormat=json (fix #7620) (#7649)
  Add back HttpSigningConfiguration.cs
  remove HTTPSigningConfiguration.cs
  add AnyType support to Swift generators (#7644)
  fix warning, remove trailing spaces (#7659)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants