swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[JavaScript] Model default value of `null` for strings results in `"null"` value

Open dganoff opened this issue 9 years ago • 4 comments

Description

Model default value of null for strings gets set to "null" upon creating a new instance of the model.

Swagger-codegen version

2.3.0

Swagger declaration file content or url

Schema definition for the model property:

outbound_localization : {
  "default": null,
  "description": "...",
  "type": "string"
}
Command line used for generation
docker run --rm -v ${PWD}:/local swaggerapi/swagger-codegen-cli generate \
      -i https://apidev.telnyx.com/mission_control_docs \
      -l javascript \
      -o /local/src/.swagger-codegen-output/missionControl \
      --additional-properties usePromises=true,useEs6=true
Steps to reproduce
  1. Create a new version of the Connection model let connection = new moduleName.Connection();
  2. See that the connection.outbound_localization value is "null"
Suggest a fix/enhancement

I suggest that if the default value for a model property is null, then set the value to null regardless of the property type. Otherwise keep it as undefined.

dganoff avatar Oct 16 '17 14:10 dganoff

@dganoff What about not specifying "default": null as the default should already be null, right?

wing328 avatar Oct 18 '17 08:10 wing328

I remember there's a discussion about null value (default or example) before and the recommended approach is to use vendor extension to indicate the null value as default/example values are treated as string ("null")

wing328 avatar Oct 18 '17 08:10 wing328

@wing328 Thanks for the feedback! I will advocate that we do away with null default values since you make a good point that they are not helpful. I'll also look into the vendor extension which sounds interesting.

dganoff avatar Oct 18 '17 14:10 dganoff

I'll also look into the vendor extension which sounds interesting.

@dganoff We've not implemented it yet. I can show you some good starting point to add it to JS client templates.

wing328 avatar Oct 19 '17 14:10 wing328