[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
- Define a property on an object as demonstrated.
- Use the object as the schema for a JSON request body.
- Generate client code with
typescript-fetch
. - 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 Date
s, as well as on JSON deserialization.