Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Javascript] Fix for constructors not handling required fields with default values well #6649

Merged
merged 1 commit into from
Aug 20, 2020

Conversation

tray2100
Copy link
Contributor

This PR is for issue #6648. I verified that the output was as expected where for a spec that contains a required field with a default value will now look like

    _this['field'] = field || 'defaultValue';

instead of just

    _this['field'] = field;

where it forces the required field to be undefined even though we have a default value. I also noticed that the samples were out of date and there wasn't a generate script for ES5 so I've added those here. I can squash the commits if needed before merging.

PR checklist

  • Read the contribution guidelines.
  • If contributing template-only or documentation-only changes which will change sample output, build the project beforehand.
  • Run the shell script ./bin/generate-samples.shto update all Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/config/java*. For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.

cc: @CodeNinjai @frol @cliffano

@wing328
Copy link
Member

wing328 commented Jun 14, 2020

@tray2100 thanks for the PR. If a field is required, there shouldn't be any default value since the value must be provided.

In other words, the default value is for the optional field, right?

@tray2100
Copy link
Contributor Author

@tray2100 thanks for the PR. If a field is required, there shouldn't be any default value since the value must be provided.

In other words, the default value is for the optional field, right?

Hey @wing328

I probably should have shared more of my use case. I've defined a oneOf entity and made the discriminator required in the schemas referenced in it. It feels super awkward right now when using the constructFromObject mechanism because you'll have to manually add in the discriminator after the object is constructed - even if you used the entity created by the generator in the first place. It's even more awkward if there is a chain of constructFromObject calls and the entity with the discriminator is a few levels down the object graph.

Setting the field to the default value if nothing is specified solves this problem in a minimalist way.

This is essentially the spec:

 OneOfDogCat:
      oneOf:
      - $ref: "#/components/schemas/Dog"
      - $ref: "#/components/schemas/Cat"
      type: "object"
      discriminator:
        propertyName: "entityType"
 Dog:
      type: "object"
      properties:
        entityType:
          default: "dog"
          type: "string"
        name:
          type: "string"
      required:
      - "entityType"
 Cat:
      type: "object"
      properties:
        entityType:
          default: "cat"
          type: "string"
        name:
          type: "string"
      required:
      - "entityType"

…values

for required fields better. This change ensures the required field isn't
overridden with undefined when the object is constructed - especially through
a chain of constructFromObject calls..
@tray2100 tray2100 force-pushed the javascript-default branch from 0778a09 to 0c5f34e Compare August 19, 2020 16:07
@wing328
Copy link
Member

wing328 commented Aug 20, 2020

@tray2100 as discussed we can use {{#oneOf}} .. {{/oneOf}} to further enhance this fix for oneOf models only. Let's see if the community can help out with this.

@wing328 wing328 merged commit 71321bd into OpenAPITools:master Aug 20, 2020
@wing328 wing328 added this to the 5.0.0 milestone Aug 20, 2020
jimschubert added a commit that referenced this pull request Aug 23, 2020
* master: (720 commits)
  [docs] Update README badges (#7276)
  Update apiInvoker.mustache and sample file for akka-scala client for issue #7258 fix (#7259)
  [Dart] Get all enum values in a list (#7166)
  Update .gitattributes
  [ci] Set ubuntu workflow verification to autoclrf=true, safeclrf=false
  Update check-supported-versions.yaml
  [ci] Update gitattributes and allow skipping docs generation for Windows CI workflows (#7273)
  [core][bug] FILES is now path relative with no prefixes (#7271)
  Update check-supported-versions.yaml
  Update check-supported-versions.yaml (#7268)
  [Java][jersey2] Add jersey injection dependencies (#7240)
  [C][Clang Static Analyzer] Remove the useless variable when assembling URL (#7255)
  Date format dart (#6389)
  minor enhancement to java client generator (#7253)
  typescript: Fix Union Types Import Issue (#6789)
  Modifying the es5 and es6 templates for javascript to handle default values (#6649)
  [python-exp] simplify examples (#7157)
  Support for KumuluzEE microprofile runtime (#5944)
  [C#][netcore] minor improvements and bug fixes (#7244)
  Deprecate Flash (ActionScript) client generator (#7231)
  ...
jimschubert added a commit to mohamedelhabib/openapi-generator that referenced this pull request Aug 24, 2020
* master: (219 commits)
  [java] Appropriate instantiation of model with dynamic properties (OpenAPITools#6052)
  [docs] Update README badges (OpenAPITools#7276)
  Update apiInvoker.mustache and sample file for akka-scala client for issue OpenAPITools#7258 fix (OpenAPITools#7259)
  [Dart] Get all enum values in a list (OpenAPITools#7166)
  Update .gitattributes
  [ci] Set ubuntu workflow verification to autoclrf=true, safeclrf=false
  Update check-supported-versions.yaml
  [ci] Update gitattributes and allow skipping docs generation for Windows CI workflows (OpenAPITools#7273)
  [core][bug] FILES is now path relative with no prefixes (OpenAPITools#7271)
  Update check-supported-versions.yaml
  Update check-supported-versions.yaml (OpenAPITools#7268)
  [Java][jersey2] Add jersey injection dependencies (OpenAPITools#7240)
  [C][Clang Static Analyzer] Remove the useless variable when assembling URL (OpenAPITools#7255)
  Date format dart (OpenAPITools#6389)
  minor enhancement to java client generator (OpenAPITools#7253)
  typescript: Fix Union Types Import Issue (OpenAPITools#6789)
  Modifying the es5 and es6 templates for javascript to handle default values (OpenAPITools#6649)
  [python-exp] simplify examples (OpenAPITools#7157)
  Support for KumuluzEE microprofile runtime (OpenAPITools#5944)
  [C#][netcore] minor improvements and bug fixes (OpenAPITools#7244)
  ...
@wing328 wing328 changed the title [Javascript] Fix for constructors not handling required fields with default values well #6648 [Javascript] Fix for constructors not handling required fields with default values well Sep 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants