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
  • #2323
Closed
Open
Issue created Mar 07, 2019 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[BUG][typescript-fetch] Optional array properties containing non-primitive elements produce TypeError on JSON serialization

Created by: Dijky

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 (closed) 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.

Assignee
Assign to
Time tracking