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-fetch] Optional array properties containing non-primitive elements produce TypeError on JSON serialization #2323

Closed
5 of 6 tasks
Dijky opened this issue Mar 7, 2019 · 1 comment

Comments

@Dijky
Copy link
Contributor

Dijky commented Mar 7, 2019

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?
  • [Optional] Bounty to sponsor the fix (example)
Description

When an object property ...

  • is not required and
  • is an array of non-primitive elements (e.g. array of objects) and
  • is serialized to JSON (e.g. when used in a request body) and
  • is not set/set to undefined (i.e. omitted)

... then the generated {{classname}}ToJSON function will produce a TypeError on run-time because Array.prototype.map() is called on the undefined property.

Runtime Error Message
TypeError: value.replacements is undefined
openapi-generator version

Tested on v4.0.0-beta2. No relevant changes have been made since then.

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Test API
  version: 0.0.1
paths:
  /someting:
    post:
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/RequestBody'
      responses:
        '200':
          description: OK
components:
  schemas:
    RequestBody:
      type: object
      properties:
        replacements:
          type: array
          items:
            type: object
            properties:
              key:
                type: string
              value:
                type: string
Command line used for generation

openapi-generator-cli -g typescript-fetch -i openapi.yml -o src/api/

Steps to reproduce
  1. Define a property on an object as demonstrated.
  2. Use the object as the schema for a JSON request body.
  3. Generate client code with typescript-fetch.
  4. Call the API method with the property in question left undefined.
Related issues/PRs

This bug was introduced with the refactoring in #569.
I'm aware that #802 will probably deprecate this eventually.

Suggest a fix

A PR will follow soon.

Idea: The call to Array.prototype.map() needs to be guarded against undefined.
This is already done on other optional properties such as Dates, as well as on JSON deserialization.

@auto-labeler
Copy link

auto-labeler bot commented Mar 7, 2019

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

Dijky added a commit to Dijky/openapi-generator that referenced this issue Mar 7, 2019
wing328 pushed a commit that referenced this issue Mar 8, 2019
…array model properties (#2324)

* [typescript-fetch] Guard array mapping against undefined on optional array model properties (#2323)

* [typescript-fetch] Update samples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant