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]: objects as query parameters break when fields are null #5174

Closed
5 tasks done
daniel-frak opened this issue Jan 31, 2020 · 3 comments · Fixed by #5197
Closed
5 tasks done
Milestone

Comments

@daniel-frak
Copy link
Contributor

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
Description

When using the "objects as query parameter" support introduced in #4407, NULL fields cause an exception TypeError: Cannot convert undefined or null to object in the addToHttpParamsRecursive function of the generated service.

openapi-generator version

4.2.3

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: Test API
  version: v0.0.1
servers:
- url: http://localhost:8080
paths:
  "/myobjects/filtered":
    get:
      operationId: findAllByFilter
      parameters:
      - name: filter
        in: query
        required: true
        schema:
          "$ref": "#/components/schemas/MyObjectFilter"
      responses:
        '200':
          description: default response
          content:
            "*/*":
              schema:
                type: "string"
components:
  schemas:
    MyObjectFilter:
      type: object
      properties:
        query:
          type: string
        type:
          type: string
          enum:
          - TYPE1
          - TYPE2
Command line used for generation
openapi-generator generate -g typescript-angular -o .generated -i .swagger-spec.json
Steps to reproduce
  1. Generate the code and copy it to an Angular application
  2. Create a new MyObjectFilter with query==null:
const filter = {
  query: null,
  type: "TYPE1"
};
  1. Try to use the service:
this.myObjectService.findAllByFilter(myObjectFilter)
Related issues/PRs

None found.

Suggest a fix

I have found that adding a NULL-check in the generated addToHttpParamsRecursive method seems to fix the issue:

private addToHttpParamsRecursive(httpParams: HttpParams, value: any, key?: string): HttpParams {
  if(value == null) {
    return httpParams;
  }
  // Original code starts here...
}
@macjohnny
Copy link
Member

@daniel-frak would you like to file a PR to fix this issue?

@macjohnny
Copy link
Member

cc @aanno2

daniel-frak added a commit to daniel-frak/openapi-generator that referenced this issue Feb 3, 2020
daniel-frak added a commit to daniel-frak/openapi-generator that referenced this issue Feb 3, 2020
@daniel-frak
Copy link
Contributor Author

I've filed #5197, let me know if I did this right :)

daniel-frak added a commit to daniel-frak/openapi-generator that referenced this issue Feb 3, 2020
daniel-frak added a commit to daniel-frak/openapi-generator that referenced this issue Feb 3, 2020
daniel-frak added a commit to daniel-frak/openapi-generator that referenced this issue Feb 3, 2020
macjohnny pushed a commit that referenced this issue Feb 3, 2020
…n fields are null (#5197)

* #5174 fix [BUG] [typescript-angular]: objects as query parameters break when fields are null

* #5174 Shell scripts run

* #5174 Code style fix

* #5174 Regenerate typescript-angular samples

* #5174 Regenerate typescript-angular samples a second time
@macjohnny macjohnny added this to the 4.3.0 milestone Feb 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants