swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.04k stars 6.03k forks source link

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

Open dganoff opened 7 years ago

dganoff commented 7 years ago
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.

wing328 commented 7 years ago

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

wing328 commented 7 years ago

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")

dganoff commented 7 years ago

@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.

wing328 commented 7 years ago

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.