OpenAPITools / OpenAPITools/openapi-generator

[BUG] Javascript does not include default values for model fields

Open
#2,637 0 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

Javascript does not include default values for model fields during serialization. This issue seems to only occur if the object is nested in a list, otherwise it works as expected.

openapi-generator version

3.3.4

OpenAPI declaration file content or url
openapi: "3.0.0"
info:
  version: 1.0.0
  title: dogstore
  license:
    name: MIT

paths:
  /products:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/DogRequest'
        required: true
      responses:
        '200':
          description: OK
components:
  schemas:
    DogRequest:
      type: object
      required:
        - dogs
      properties:
        dogs:
          type: array
          items:
            oneOf:
              - $ref: '#/components/schemas/GoldenRetriever'
              - $ref: '#/components/schemas/LabradorRetriever'
        age:
          type: integer

    GoldenRetriever:
      type: object
      required:
        - weight
      properties:
        weight:
          type: integer
        slug:
          type: string
          default: "golden"

    LabradorRetriever:
       type: object
       required:
         - weight
       properties:
         slug:
           type: string
           default: "labrador"
         weight:
           type: integer
Steps to reproduce
var DogStore = require('dogstore');

var api = new DogStore.DefaultApi()
var dogRequest = new DogStore.DogRequest(); // {DogRequest} 

var golden = DogStore.GoldenRetriever.constructFromObject({'weight': 35});
var labrador = DogStore.LabradorRetriever.constructFromObject({'weight': 40});

dogRequest.dogs = [golden, labrador];

var callback = function(error, data, response) {
  if (error) {
    console.error(error);
  } else {
    console.log('API called successfully.');
  }
};
api.productsPost(dogRequest, callback);

The following HTTP JSON body is sent

{
    "dogs": [
        {
            "weight": 35
        },
        {
            "weight": 40
        }
    ]
}

whereas

{
    "dogs": [
        {   "slug": "golden",
            "weight": 35
        },
        {   "slug": "labrador",
            "weight": 40
        }
    ]
}

would be expected, so that it would include the default values for both models without specifying them in the object that's passed to constructFromObject

Related issues/PRs

Could not find any.

Suggest a fix

I solved it for my use case here: https://github.com/OpenAPITools/openapi-generator/compare/master...UkuLoskit:fix-default-value-ignored-for-javascript

However, I'm not sure if it's the right approach. Please let me know, so that I can submit the PR.

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

Start with the generated JavaScript model entry points shown in the reproduction, especially constructFromObject and productsPost, and trace serialization of nested dogs arrays and oneOf models. Compare the generated request body with the expected JSON, then add coverage for default slug values when GoldenRetriever and LabradorRetriever instances are nested in DogRequest.dogs.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.