Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/generators/typescript-angular.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|stringEnums|Generate string enums instead of objects for enum values.| |false|
|subtypeVisitor|Use discriminators to create a subtype visitor.| |false|
|supportsES6|Generate code that conforms to ES6.| |false|
|taggedUnions|Use discriminators to create tagged unions instead of extending interfaces.| |false|
|useSingleRequestParameter|Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter.| |false|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public static enum PROVIDED_IN_LEVEL {none, root, any, platform}
public static final String WITH_INTERFACES = "withInterfaces";
public static final String USE_SINGLE_REQUEST_PARAMETER = "useSingleRequestParameter";
public static final String TAGGED_UNIONS = "taggedUnions";
public static final String SUBTYPE_VISITOR = "subtypeVisitor";
public static final String NG_VERSION = "ngVersion";
public static final String PROVIDED_IN_ROOT = "providedInRoot";
public static final String PROVIDED_IN = "providedIn";
Expand Down Expand Up @@ -77,6 +78,7 @@ public static enum PROVIDED_IN_LEVEL {none, root, any, platform}
protected PROVIDED_IN_LEVEL providedIn = PROVIDED_IN_LEVEL.root;

private boolean taggedUnions = false;
private boolean subtypeVisitor = false;

public TypeScriptAngularClientCodegen() {
super();
Expand Down Expand Up @@ -106,6 +108,9 @@ public TypeScriptAngularClientCodegen() {
this.cliOptions.add(CliOption.newBoolean(TAGGED_UNIONS,
"Use discriminators to create tagged unions instead of extending interfaces.",
this.taggedUnions));
this.cliOptions.add(CliOption.newBoolean(SUBTYPE_VISITOR,
"Use discriminators to create a subtype visitor.",
this.subtypeVisitor));
this.cliOptions.add(CliOption.newBoolean(PROVIDED_IN_ROOT,
"Use this property to provide Injectables in root (it is only valid in angular version greater or equal to 6.0.0). IMPORTANT: Deprecated for angular version greater or equal to 9.0.0, use **providedIn** instead.",
false));
Expand Down Expand Up @@ -200,6 +205,10 @@ public void processOpts() {
taggedUnions = Boolean.parseBoolean(additionalProperties.get(TAGGED_UNIONS).toString());
}

if (additionalProperties.containsKey(SUBTYPE_VISITOR)) {
subtypeVisitor = Boolean.parseBoolean(additionalProperties.get(SUBTYPE_VISITOR).toString());
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you commented above, this change is incompatible with taggedUnions - could we add a check for this here, and output an error & terminate if both options are set?

I am unsure how to implement this though - maybe @macjohnny or @wing328 know more and could shed some light on how this is usually done in other generators.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation ignores the subtypeVisitor property when taggedUnions is set, it doesn't (or at least shouldn't) generate non-compilable code.

This could of course be changed in order to throw an exception if that is preferred.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think an exception or at least a warning would be useful for users

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added support for generating a visitor for tagged unions, so there is no need for this warning anymore.

if (ngVersion.atLeast("9.0.0") && additionalProperties.containsKey(PROVIDED_IN)) {
setProvidedIn(additionalProperties.get(PROVIDED_IN).toString());
} else {
Expand Down Expand Up @@ -514,11 +523,18 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
CodegenModel cm = (CodegenModel) mo.get("model");
if (taggedUnions) {
mo.put(TAGGED_UNIONS, true);
}
if (subtypeVisitor) {
mo.put(SUBTYPE_VISITOR, true);
}
if (taggedUnions || subtypeVisitor) {
if (cm.discriminator != null && cm.children != null) {
for (CodegenModel child : cm.children) {
cm.imports.add(child.classname);
}
}
}
if (taggedUnions) {
if (cm.parent != null) {
cm.imports.remove(cm.parent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export interface {{classname}}{{#allParents}}{{#-first}} extends {{/-first}}{{{.
{{/description}}
{{#isReadOnly}}readonly {{/isReadOnly}}{{{name}}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}} | null{{/isNullable}};
{{/vars}}
}{{>modelGenericEnums}}
}{{>modelGenericEnums}}{{>modelGenericVisitor}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{{#subtypeVisitor}}
{{#discriminator}}


export interface {{classname}}Visitor<R> {
{{#discriminator.mappedModels}}
visit{{modelName}}(value: {{modelName}}): R;
{{/discriminator.mappedModels}}
}

export function visit{{classname}}<R>(value: {{classname}}, visitor: {{classname}}Visitor<R>): R {
switch (value.{{discriminator.propertyName}}) {
{{#discriminator.mappedModels}}
case '{{mappingName}}':
return visitor.visit{{modelName}}(<{{modelName}}>value);
{{/discriminator.mappedModels}}
default:
{{#taggedUnions}}
// Make the compiler fail if there is an unmapped member
const _exhaustiveCheck: never = value;
throw new Error("Unknown model discriminator for '" + value + "'" + _exhaustiveCheck);
{{/taggedUnions}}
{{^taggedUnions}}
throw new Error("Unknown model discriminator '" + value.{{discriminator.propertyName}} + "'");
{{/taggedUnions}}
}
}
{{/discriminator}}
{{/subtypeVisitor}}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{{#discriminator}}
export type {{classname}} = {{#children}}{{^-first}} | {{/-first}}{{classname}}{{/children}};
{{>modelGenericVisitor}}
{{/discriminator}}
{{^discriminator}}
{{#parent}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public Map<String, String> createOptions() {
.put(TypeScriptAngularClientCodegen.PROVIDED_IN_ROOT, Boolean.FALSE.toString())
.put(TypeScriptAngularClientCodegen.PROVIDED_IN, PROVIDED_IN_LEVEL)
.put(TypeScriptAngularClientCodegen.TAGGED_UNIONS, Boolean.FALSE.toString())
.put(TypeScriptAngularClientCodegen.SUBTYPE_VISITOR, Boolean.FALSE.toString())
.put(TypeScriptAngularClientCodegen.NPM_REPOSITORY, NPM_REPOSITORY)
.put(TypeScriptAngularClientCodegen.NG_VERSION, NG_VERSION)
.put(TypeScriptAngularClientCodegen.API_MODULE_PREFIX, API_MODULE_PREFIX)
Expand Down