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

Bugfix/fix for correctly generating additional properties, added types for typescript #18491

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/generators/javascript-flowtyped.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|array|Array|
|list|Array|
|map|Object|
|set|Set|


## LANGUAGE PRIMITIVES
Expand Down
4 changes: 4 additions & 0 deletions docs/generators/typescript-angular.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
| Type/Alias | Instantiated By |
| ---------- | --------------- |
|array|Array|
|map|Record|
|set|Set|


## LANGUAGE PRIMITIVES
Expand All @@ -81,13 +83,15 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Map</li>
<li>Object</li>
<li>ReadonlyArray</li>
<li>Record</li>
<li>Set</li>
<li>String</li>
<li>any</li>
<li>boolean</li>
<li>number</li>
<li>object</li>
<li>string</li>
<li>unknown</li>
</ul>

## RESERVED WORDS
Expand Down
4 changes: 4 additions & 0 deletions docs/generators/typescript-aurelia.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
| Type/Alias | Instantiated By |
| ---------- | --------------- |
|array|Array|
|map|Record|
|set|Set|


## LANGUAGE PRIMITIVES
Expand All @@ -65,13 +67,15 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Map</li>
<li>Object</li>
<li>ReadonlyArray</li>
<li>Record</li>
<li>Set</li>
<li>String</li>
<li>any</li>
<li>boolean</li>
<li>number</li>
<li>object</li>
<li>string</li>
<li>unknown</li>
</ul>

## RESERVED WORDS
Expand Down
4 changes: 4 additions & 0 deletions docs/generators/typescript-axios.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
| Type/Alias | Instantiated By |
| ---------- | --------------- |
|array|Array|
|map|Record|
|set|Set|


## LANGUAGE PRIMITIVES
Expand All @@ -73,13 +75,15 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Map</li>
<li>Object</li>
<li>ReadonlyArray</li>
<li>Record</li>
<li>Set</li>
<li>String</li>
<li>any</li>
<li>boolean</li>
<li>number</li>
<li>object</li>
<li>string</li>
<li>unknown</li>
</ul>

## RESERVED WORDS
Expand Down
4 changes: 4 additions & 0 deletions docs/generators/typescript-fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
| Type/Alias | Instantiated By |
| ---------- | --------------- |
|array|Array|
|map|Record|
|set|Set|


## LANGUAGE PRIMITIVES
Expand All @@ -73,13 +75,15 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Map</li>
<li>Object</li>
<li>ReadonlyArray</li>
<li>Record</li>
<li>Set</li>
<li>String</li>
<li>any</li>
<li>boolean</li>
<li>number</li>
<li>object</li>
<li>string</li>
<li>unknown</li>
</ul>

## RESERVED WORDS
Expand Down
4 changes: 4 additions & 0 deletions docs/generators/typescript-inversify.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
| Type/Alias | Instantiated By |
| ---------- | --------------- |
|array|Array|
|map|Record|
|set|Set|


## LANGUAGE PRIMITIVES
Expand All @@ -71,13 +73,15 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Map</li>
<li>Object</li>
<li>ReadonlyArray</li>
<li>Record</li>
<li>Set</li>
<li>String</li>
<li>any</li>
<li>boolean</li>
<li>number</li>
<li>object</li>
<li>string</li>
<li>unknown</li>
</ul>

## RESERVED WORDS
Expand Down
4 changes: 4 additions & 0 deletions docs/generators/typescript-jquery.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
| Type/Alias | Instantiated By |
| ---------- | --------------- |
|array|Array|
|map|Record|
|set|Set|


## LANGUAGE PRIMITIVES
Expand All @@ -67,13 +69,15 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Map</li>
<li>Object</li>
<li>ReadonlyArray</li>
<li>Record</li>
<li>Set</li>
<li>String</li>
<li>any</li>
<li>boolean</li>
<li>number</li>
<li>object</li>
<li>string</li>
<li>unknown</li>
</ul>

## RESERVED WORDS
Expand Down
4 changes: 4 additions & 0 deletions docs/generators/typescript-nestjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
| Type/Alias | Instantiated By |
| ---------- | --------------- |
|array|Array|
|map|Record|
|set|Set|


## LANGUAGE PRIMITIVES
Expand All @@ -76,13 +78,15 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Map</li>
<li>Object</li>
<li>ReadonlyArray</li>
<li>Record</li>
<li>Set</li>
<li>String</li>
<li>any</li>
<li>boolean</li>
<li>number</li>
<li>object</li>
<li>string</li>
<li>unknown</li>
</ul>

## RESERVED WORDS
Expand Down
4 changes: 4 additions & 0 deletions docs/generators/typescript-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
| Type/Alias | Instantiated By |
| ---------- | --------------- |
|array|Array|
|map|Record|
|set|Set|


## LANGUAGE PRIMITIVES
Expand All @@ -68,6 +70,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Object</li>
<li>ReadStream</li>
<li>ReadonlyArray</li>
<li>Record</li>
<li>RequestDetailedFile</li>
<li>RequestFile</li>
<li>Set</li>
Expand All @@ -77,6 +80,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>number</li>
<li>object</li>
<li>string</li>
<li>unknown</li>
</ul>

## RESERVED WORDS
Expand Down
4 changes: 4 additions & 0 deletions docs/generators/typescript-redux-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
| Type/Alias | Instantiated By |
| ---------- | --------------- |
|array|Array|
|map|Record|
|set|Set|


## LANGUAGE PRIMITIVES
Expand All @@ -68,13 +70,15 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Map</li>
<li>Object</li>
<li>ReadonlyArray</li>
<li>Record</li>
<li>Set</li>
<li>String</li>
<li>any</li>
<li>boolean</li>
<li>number</li>
<li>object</li>
<li>string</li>
<li>unknown</li>
</ul>

## RESERVED WORDS
Expand Down
4 changes: 4 additions & 0 deletions docs/generators/typescript-rxjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
| Type/Alias | Instantiated By |
| ---------- | --------------- |
|array|Array|
|map|Record|
|set|Set|


## LANGUAGE PRIMITIVES
Expand All @@ -68,13 +70,15 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Map</li>
<li>Object</li>
<li>ReadonlyArray</li>
<li>Record</li>
<li>Set</li>
<li>String</li>
<li>any</li>
<li>boolean</li>
<li>number</li>
<li>object</li>
<li>string</li>
<li>unknown</li>
</ul>

## RESERVED WORDS
Expand Down
4 changes: 4 additions & 0 deletions docs/generators/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
| Type/Alias | Instantiated By |
| ---------- | --------------- |
|array|Array|
|map|Record|
|set|Set|


## LANGUAGE PRIMITIVES
Expand All @@ -73,13 +75,15 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Map</li>
<li>Object</li>
<li>ReadonlyArray</li>
<li>Record</li>
<li>Set</li>
<li>String</li>
<li>any</li>
<li>boolean</li>
<li>number</li>
<li>object</li>
<li>string</li>
<li>unknown</li>
</ul>

## RESERVED WORDS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2058,9 +2058,19 @@ public String generateExamplePath(String path, Operation operation) {
public String toInstantiationType(Schema schema) {
if (ModelUtils.isMapSchema(schema)) {
Schema additionalProperties = ModelUtils.getAdditionalProperties(schema);
Schema unaliasedAdditionalPropertiesSchema = unaliasSchema(additionalProperties);

String inner = getSchemaType(additionalProperties);
String mapInstantion = instantiationTypes.get("map");
if (mapInstantion != null) {
// Additional properties cannot be composed (allof, anyof, oneof)
if (!ModelUtils.isComposedSchema(additionalProperties)
&& !ModelUtils.isRefToSchemaWithProperties(unaliasedAdditionalPropertiesSchema.get$ref())
&& StringUtils.isBlank(unaliasedAdditionalPropertiesSchema.get$ref())
) {
String primitiveAdditionalPropertiesType = getPrimitiveType(unaliasedAdditionalPropertiesSchema);
if ("object".equals(primitiveAdditionalPropertiesType)) {
inner = typeMapping.get("AnyType");
}
return mapInstantion + "<String, " + inner + ">";
}
return inner;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,18 @@ public AbstractTypeScriptClientCodegen() {
"Error",
"Map",
"object",
"Set"
"Set",
"Record",
"unknown"
));

languageGenericTypes = new HashSet<>(Collections.singletonList(
"Array"
));

instantiationTypes.put("array", "Array");
instantiationTypes.put("map", "Record");
instantiationTypes.put("set", "Set");

typeMapping = new HashMap<String, String>();
typeMapping.put("Set", "Set");
Expand All @@ -340,18 +344,18 @@ public AbstractTypeScriptClientCodegen() {
typeMapping.put("double", "number");
typeMapping.put("object", "object");
typeMapping.put("integer", "number");
typeMapping.put("Map", "any");
typeMapping.put("map", "any");
typeMapping.put("Map", "unknown");
typeMapping.put("map", "unknown");
typeMapping.put("date", "string");
typeMapping.put("DateTime", "string");
typeMapping.put("binary", "any");
typeMapping.put("File", "any");
typeMapping.put("file", "any");
typeMapping.put("binary", "unknown");
typeMapping.put("File", "unknown");
typeMapping.put("file", "unknown");
typeMapping.put("ByteArray", "string");
typeMapping.put("UUID", "string");
typeMapping.put("URI", "string");
typeMapping.put("Error", "Error");
typeMapping.put("AnyType", "any");
typeMapping.put("AnyType", "unknown");

cliOptions.add(new CliOption(CodegenConstants.ENUM_NAME_SUFFIX, CodegenConstants.ENUM_NAME_SUFFIX_DESC).defaultValue(this.enumSuffix));
cliOptions.add(new CliOption(CodegenConstants.ENUM_PROPERTY_NAMING, CodegenConstants.ENUM_PROPERTY_NAMING_DESC).defaultValue(this.enumPropertyNaming.name()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public ModelsMap postProcessModels(ModelsMap objs) {
CodegenModel cm = mo.getModel();

// Type is already any
if (cm.getAdditionalPropertiesIsAnyType() && "any".equals(cm.getAdditionalPropertiesType())) {
if (cm.getAdditionalPropertiesIsAnyType() && "unknown".equals(cm.getAdditionalPropertiesType())) {
cm.setAdditionalPropertiesIsAnyType(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public TypeScriptClientCodegen() {
"constructor"));

typeMapping.put("List", "Array");
typeMapping.put("object", "any");
typeMapping.put("object", "object");
typeMapping.put("DateTime", "Date");

cliOptions.add(new CliOption(NPM_REPOSITORY, "Use this property to set an url your private npmRepo in the package.json"));
Expand Down Expand Up @@ -494,7 +494,7 @@ public String getTypeDeclaration(Schema p) {
} else if (ModelUtils.isFileSchema(p)) {
return "HttpFile";
} else if (ModelUtils.isBinarySchema(p)) {
return "any";
return "unknown";
} else {
return super.getTypeDeclaration(p);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
export type {{classname}} = {
{{#additionalPropertiesType}}
[key: string]: {{{additionalPropertiesType}}}{{#hasVars}} | any{{/hasVars}};
[key: string]: {{{additionalPropertiesType}}}{{#hasVars}} | unknown{{/hasVars}};
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if its a good idea to use unknown instead of any, since this probably breaks a lot of client code that accesses these values

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From the typescript perspective (ECMA standarts etc) it is better to use unknown, as it is exactly describe its state/type, which is unknown and you must check/know the type of it. IMO it make the code more error-free/robust in runtime as you will be forced to check the type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can put it back to any but my linter and strict tsconfig settings do not like it, which is sign of not best typing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or maybe, make use of config switch supportsES6 that when it would be true, it would use unknown instead of any

Copy link
Member

Choose a reason for hiding this comment

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

i would suggest to switch it back to any, in order not to break existing use cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for late reply, had a lot in work... I do not agree with that completely, preserving backwards compatibility si for sure important, but in our codebase, any type will fail in every project as we use strict typing config from official ts configs package. I suggest to add config switch to generator that we will be able to set if any of unknown, as default there would be any so preserving backwards compatibility, what do you think? @macjohnny @andreseloysv

Copy link
Member

Choose a reason for hiding this comment

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

applying linting/strict typing rules etc. to generated code is a hard one anyway. you can always just have a custom script to post-process the output and replace any with unknown

a config switch sounds a bit like an overkill here, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@macjohnny We use that, because we have strict typing rule in our org :/ and tbh imo, having script to post-process the output and replace any with unknown is overkill as you would change already generated code, instead, with switch, you would just already set what will be generated in the end. In @sunray-eu We would really love to have this switch available. Let me know your final decision if it would be to big change in code with this switch adding, and then, we can finally merge it. Thanks!

Copy link
Member

@macjohnny macjohnny Jun 11, 2024

Choose a reason for hiding this comment

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

*shrug* ok lets have the switch. an alternative though would be to generate the code into a separate npm package, and import that package in your project. this way, you dont need to apply your projects build rules.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have alternative package, of course, but 1. there is also build, 2. when we use it in other package, the return data type is sometimes any, which in lot of cases disable type check


{{/additionalPropertiesType}}
{{#vars}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{#additionalPropertiesType}}

[key: string]: {{{additionalPropertiesType}}}{{#hasVars}} | any{{/hasVars}};
[key: string]: {{{additionalPropertiesType}}}{{#hasVars}} | unknown{{/hasVars}};

{{/additionalPropertiesType}}