OpenAPITools / OpenAPITools/openapi-generator

[BUG] Typescript-angular: JSON query params are not serialized as JSON

Open
#21,934 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

With the Typescript-angular generator, JSON query parameters are not serialized as JSON.
Instead each field of the object is added independently.

Example (see OpenAPI specification below),

For the following call

const filter = {name: 'John', age: 37};
service.search(filter).subscribe();

I expect a request to http://localhost/search?filter=%7B%22name%22%3A%22John%22%2C%22age%22%3A37%7D (URL encoded JSON)

But instead it requests http://localhost/search?name=John&age=37

On this branch, I've started to implement tests showing the issue and trying to implement a solution.

openapi-generator version

7.15.0 (was working with 7.4.0, I do not know in which version it stopped to work).

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: query-param-json
  version: 1.0.0
paths:
  /search:
    get:
      operationId: search
      parameters:
        - in: query
          name: filter
          description: Filter
          required: false
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Filter'

      responses:
        '200':
          description: Ok
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Response'

components:
  schemas:
    Item:
      type: object
      required:
        - name
        - age
      properties:
        name:
          type: string
          description: The name
        age:
          type: integer
          minimum: 0
          default: 5
          nullable: false
          description: The age

    Response:
      type: object
      required:
        - items
      properties:
        items:
          type: array
          description: Array of items.
          nullable: false
          items:
            $ref: '#/components/schemas/Item'

    Filter:
      type: object
      properties:
        name:
          type: string
          description: The name
        age:
          type: integer
          minimum: 0
          default: 5
          nullable: false
          description: The age

Generation Details

Typescript-angular

Steps to reproduce

Generate and call the search operation of the service, query params will not be JSON

Related issues/PRs

One must be careful, there seems to have been already several issues regarding the serialization of query parameters for this generator.
A few recent

Suggest a fix

The issue is that when the method addToHttpParams() of the file modules/openapi-generator/src/main/resources/typescript-angular/api.base.service.mustache is called, we have already lost the information that the parameter must be in JSON.
In some way, this must be provided from `modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache``

I've started to implement a fix in https://github.com/OpenAPITools/openapi-generator/compare/master...VladimirSvoboda:openapi-generator:AngularJsonQueryParam

However, due to the lack of tests regarding all the kind of different query parameters serialization, there is a risk that I introduce other regressions.
The fix is not ready yet as I have observed a regression.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read the generated-parameter flow in modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache and addToHttpParams() in api.base.service.mustache. Review the tests started on the reporter's AngularJsonQueryParam branch, then expand coverage for JSON query parameters and existing serialization cases. Done means the example request uses URL-encoded JSON without regressions in other query parameter forms.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.