Skip to content

Commit

Permalink
[typescript-node] Set model default values (#10262)
Browse files Browse the repository at this point in the history
* fix: add default value, if present, for model properties (closes #10261)

* update samples

* fix: set default to null to allow #defaultValue to work in templates

* fix: escape the string default value, leverage super rather than copy-pasta

* update samples again
  • Loading branch information
sarumont committed Sep 30, 2021
1 parent c09c626 commit 8c059a8
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ public String toDefaultValue(Schema p) {
return UNDEFINED_VALUE;
} else if (ModelUtils.isStringSchema(p)) {
if (p.getDefault() != null) {
return "'" + (String) p.getDefault() + "'";
return "'" + escapeText((String) p.getDefault()) + "'";
}
return UNDEFINED_VALUE;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ private String getApiFilenameFromClassname(String classname) {
return toApiFilename(name);
}

@Override
@Override
protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Schema schema) {
super.addAdditionPropertiesToCodeGenModel(codegenModel, schema);
Schema additionalProperties = getAdditionalProperties(schema);
Expand All @@ -319,4 +319,13 @@ protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Sc
}
addImport(codegenModel, codegenModel.additionalPropertiesType);
}

@Override
public String toDefaultValue(Schema p) {
String def = super.toDefaultValue(p);
if ("undefined".equals(def)) {
return null;
}
return def;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{
* {{{.}}}
*/
{{/description}}
'{{name}}'{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}};
'{{name}}'{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
{{/vars}}

{{#discriminator}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ public void simpleModelTest() {
Assert.assertEquals(property1.baseName, "id");
Assert.assertEquals(property1.dataType, "number");
Assert.assertEquals(property1.name, "id");
Assert.assertEquals(property1.defaultValue, "undefined");
Assert.assertEquals(property1.defaultValue, null);
Assert.assertEquals(property1.baseType, "number");
Assert.assertTrue(property1.required);

final CodegenProperty property2 = cm.vars.get(1);
Assert.assertEquals(property2.baseName, "name");
Assert.assertEquals(property2.dataType, "string");
Assert.assertEquals(property2.name, "name");
Assert.assertEquals(property2.defaultValue, "undefined");
Assert.assertEquals(property2.defaultValue, null);
Assert.assertEquals(property2.baseType, "string");
Assert.assertTrue(property2.required);

Expand All @@ -85,23 +85,23 @@ public void simpleModelTest() {
Assert.assertEquals(property3.complexType, null);
Assert.assertEquals(property3.dataType, "Date");
Assert.assertEquals(property3.name, "createdAt");
Assert.assertEquals(property3.defaultValue, "undefined");
Assert.assertEquals(property3.defaultValue, null);
Assert.assertFalse(property3.required);

final CodegenProperty property4 = cm.vars.get(3);
Assert.assertEquals(property4.baseName, "birthDate");
Assert.assertEquals(property4.complexType, null);
Assert.assertEquals(property4.dataType, "string");
Assert.assertEquals(property4.name, "birthDate");
Assert.assertEquals(property4.defaultValue, "undefined");
Assert.assertEquals(property4.defaultValue, null);
Assert.assertFalse(property4.required);

final CodegenProperty property5 = cm.vars.get(4);
Assert.assertEquals(property5.baseName, "active");
Assert.assertEquals(property5.complexType, null);
Assert.assertEquals(property5.dataType, "boolean");
Assert.assertEquals(property5.name, "active");
Assert.assertEquals(property5.defaultValue, "undefined");
Assert.assertEquals(property5.defaultValue, null);
Assert.assertFalse(property5.required);
Assert.assertFalse(property5.isContainer);
}
Expand Down Expand Up @@ -187,7 +187,7 @@ public void listPropertyTest() {
Assert.assertEquals(property1.baseName, "id");
Assert.assertEquals(property1.dataType, "number");
Assert.assertEquals(property1.name, "id");
Assert.assertEquals(property1.defaultValue, "undefined");
Assert.assertEquals(property1.defaultValue, null);
Assert.assertEquals(property1.baseType, "number");
Assert.assertTrue(property1.required);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Order {
* Order Status
*/
'status'?: Order.StatusEnum;
'complete'?: boolean;
'complete'?: boolean = false;

static discriminator: string | undefined = undefined;

Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/typescript-node/npm/model/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Order {
* Order Status
*/
'status'?: Order.StatusEnum;
'complete'?: boolean;
'complete'?: boolean = false;

static discriminator: string | undefined = undefined;

Expand Down

0 comments on commit 8c059a8

Please sign in to comment.