mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-19 01:57:05 +00:00
[typescript] Make additional properties access safer (#5207)
* [typescript-fetch] Make additional properties access safer Instead of asserting that any key access returns a valid property, force the consumer to check that the value is defined. * Update tests * Put null-safe additional props behind and flag and share * Undo over copy * Update docs * Rearrange code * Move to unit tests
This commit is contained in:
committed by
GitHub
parent
2aa8a6d033
commit
a8015ad8c1
@@ -55,6 +55,8 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
|
||||
public static final String ENUM_NAME_SUFFIX_DESC_CUSTOMIZED = CodegenConstants.ENUM_NAME_SUFFIX_DESC
|
||||
+ " A special '" + ENUM_NAME_SUFFIX_V4_COMPAT + "' value enables the backward-compatible behavior (as pre v4.2.3)";
|
||||
|
||||
public static final String NULL_SAFE_ADDITIONAL_PROPS = "nullSafeAdditionalProps";
|
||||
public static final String NULL_SAFE_ADDITIONAL_PROPS_DESC = "Set to make additional properties types declare that their indexer may return undefined";
|
||||
|
||||
// NOTE: SimpleDateFormat is not thread-safe and may not be static unless it is thread-local
|
||||
@SuppressWarnings("squid:S5164")
|
||||
@@ -63,6 +65,7 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
|
||||
protected String modelPropertyNaming = "camelCase";
|
||||
protected ENUM_PROPERTY_NAMING_TYPE enumPropertyNaming = ENUM_PROPERTY_NAMING_TYPE.PascalCase;
|
||||
protected Boolean supportsES6 = false;
|
||||
protected Boolean nullSafeAdditionalProps = false;
|
||||
protected HashSet<String> languageGenericTypes;
|
||||
protected String npmName = null;
|
||||
protected String npmVersion = "1.0.0";
|
||||
@@ -174,6 +177,7 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
|
||||
this.cliOptions.add(CliOption.newBoolean(SNAPSHOT,
|
||||
"When setting this property to true, the version will be suffixed with -SNAPSHOT." + this.SNAPSHOT_SUFFIX_FORMAT.get().toPattern(),
|
||||
false));
|
||||
this.cliOptions.add(new CliOption(NULL_SAFE_ADDITIONAL_PROPS, NULL_SAFE_ADDITIONAL_PROPS_DESC).defaultValue(String.valueOf(this.getNullSafeAdditionalProps())));
|
||||
|
||||
}
|
||||
|
||||
@@ -204,6 +208,10 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
|
||||
additionalProperties.put("supportsES6", getSupportsES6());
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(NULL_SAFE_ADDITIONAL_PROPS)) {
|
||||
setNullSafeAdditionalProps(Boolean.valueOf(additionalProperties.get(NULL_SAFE_ADDITIONAL_PROPS).toString()));
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(NPM_NAME)) {
|
||||
this.setNpmName(additionalProperties.get(NPM_NAME).toString());
|
||||
}
|
||||
@@ -380,7 +388,8 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
|
||||
return getSchemaType(p) + "<" + getTypeDeclaration(inner) + ">";
|
||||
} else if (ModelUtils.isMapSchema(p)) {
|
||||
Schema inner = ModelUtils.getAdditionalProperties(p);
|
||||
return "{ [key: string]: " + getTypeDeclaration(inner) + "; }";
|
||||
String nullSafeSuffix = getNullSafeAdditionalProps() ? " | undefined" : "";
|
||||
return "{ [key: string]: " + getTypeDeclaration(inner) + nullSafeSuffix + "; }";
|
||||
} else if (ModelUtils.isFileSchema(p)) {
|
||||
return "any";
|
||||
} else if (ModelUtils.isBinarySchema(p)) {
|
||||
@@ -723,6 +732,14 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
|
||||
return supportsES6;
|
||||
}
|
||||
|
||||
public Boolean getNullSafeAdditionalProps() {
|
||||
return nullSafeAdditionalProps;
|
||||
}
|
||||
|
||||
public void setNullSafeAdditionalProps(Boolean value) {
|
||||
nullSafeAdditionalProps = value;
|
||||
}
|
||||
|
||||
public String getNpmName() {
|
||||
return npmName;
|
||||
}
|
||||
|
||||
@@ -134,14 +134,7 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
|
||||
|
||||
@Override
|
||||
public String getTypeDeclaration(Schema p) {
|
||||
Schema inner;
|
||||
if (ModelUtils.isArraySchema(p)) {
|
||||
inner = ((ArraySchema) p).getItems();
|
||||
return this.getSchemaType(p) + "<" + this.getTypeDeclaration(inner) + ">";
|
||||
} else if (ModelUtils.isMapSchema(p)) {
|
||||
inner = ModelUtils.getAdditionalProperties(p);
|
||||
return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }";
|
||||
} else if (ModelUtils.isFileSchema(p)) {
|
||||
if (ModelUtils.isFileSchema(p)) {
|
||||
return "Blob";
|
||||
} else if (ModelUtils.isBinarySchema(p)) {
|
||||
return "Blob";
|
||||
|
||||
@@ -103,14 +103,7 @@ public class TypeScriptReduxQueryClientCodegen extends AbstractTypeScriptClientC
|
||||
|
||||
@Override
|
||||
public String getTypeDeclaration(Schema p) {
|
||||
Schema inner;
|
||||
if (ModelUtils.isArraySchema(p)) {
|
||||
inner = ((ArraySchema) p).getItems();
|
||||
return this.getSchemaType(p) + "<" + this.getTypeDeclaration(inner) + ">";
|
||||
} else if (ModelUtils.isMapSchema(p)) {
|
||||
inner = ModelUtils.getAdditionalProperties(p);
|
||||
return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }";
|
||||
} else if (ModelUtils.isFileSchema(p)) {
|
||||
if (ModelUtils.isFileSchema(p)) {
|
||||
return "Blob";
|
||||
} else if (ModelUtils.isBinarySchema(p)) {
|
||||
return "Blob";
|
||||
|
||||
@@ -109,14 +109,7 @@ public class TypeScriptRxjsClientCodegen extends AbstractTypeScriptClientCodegen
|
||||
|
||||
@Override
|
||||
public String getTypeDeclaration(Schema p) {
|
||||
Schema inner;
|
||||
if (ModelUtils.isArraySchema(p)) {
|
||||
inner = ((ArraySchema) p).getItems();
|
||||
return this.getSchemaType(p) + "<" + this.getTypeDeclaration(inner) + ">";
|
||||
} else if (ModelUtils.isMapSchema(p)) {
|
||||
inner = ModelUtils.getAdditionalProperties(p);
|
||||
return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }";
|
||||
} else if (ModelUtils.isFileSchema(p)) {
|
||||
if (ModelUtils.isFileSchema(p)) {
|
||||
return "Blob";
|
||||
} else if (ModelUtils.isBinarySchema(p)) {
|
||||
return "Blob";
|
||||
|
||||
@@ -19,12 +19,14 @@ package org.openapitools.codegen.options;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.openapitools.codegen.CodegenConstants;
|
||||
import org.openapitools.codegen.languages.AbstractTypeScriptClientCodegen;
|
||||
import org.openapitools.codegen.languages.TypeScriptAngularClientCodegen;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class TypeScriptAngularClientOptionsProvider implements OptionsProvider {
|
||||
public static final String SUPPORTS_ES6_VALUE = "false";
|
||||
public static final String NULL_SAFE_ADDITIONAL_PROPS_VALUE = "false";
|
||||
public static final String ENUM_NAME_SUFFIX = "Enum";
|
||||
public static final String STRING_ENUMS_VALUE = "false";
|
||||
public static final String SORT_PARAMS_VALUE = "false";
|
||||
@@ -59,6 +61,7 @@ public class TypeScriptAngularClientOptionsProvider implements OptionsProvider {
|
||||
.put(CodegenConstants.ENUM_PROPERTY_NAMING, ENUM_PROPERTY_NAMING_VALUE)
|
||||
.put(CodegenConstants.MODEL_PROPERTY_NAMING, MODEL_PROPERTY_NAMING_VALUE)
|
||||
.put(CodegenConstants.SUPPORTS_ES6, SUPPORTS_ES6_VALUE)
|
||||
.put(AbstractTypeScriptClientCodegen.NULL_SAFE_ADDITIONAL_PROPS, NULL_SAFE_ADDITIONAL_PROPS_VALUE)
|
||||
.put(CodegenConstants.ENUM_NAME_SUFFIX, ENUM_NAME_SUFFIX)
|
||||
.put(TypeScriptAngularClientCodegen.STRING_ENUMS, STRING_ENUMS_VALUE)
|
||||
.put(TypeScriptAngularClientCodegen.NPM_NAME, NMP_NAME)
|
||||
|
||||
@@ -19,11 +19,13 @@ package org.openapitools.codegen.options;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.openapitools.codegen.CodegenConstants;
|
||||
import org.openapitools.codegen.languages.AbstractTypeScriptClientCodegen;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class TypeScriptAngularJsClientOptionsProvider implements OptionsProvider {
|
||||
public static final String SUPPORTS_ES6_VALUE = "false";
|
||||
public static final String NULL_SAFE_ADDITIONAL_PROPS_VALUE = "false";
|
||||
public static final String ENUM_NAME_SUFFIX = "Enum";
|
||||
public static final String SORT_PARAMS_VALUE = "false";
|
||||
public static final String SORT_MODEL_PROPERTIES_VALUE = "false";
|
||||
@@ -44,6 +46,7 @@ public class TypeScriptAngularJsClientOptionsProvider implements OptionsProvider
|
||||
return builder.put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, SORT_PARAMS_VALUE)
|
||||
.put(CodegenConstants.SORT_MODEL_PROPERTIES_BY_REQUIRED_FLAG, SORT_MODEL_PROPERTIES_VALUE)
|
||||
.put(CodegenConstants.SUPPORTS_ES6, SUPPORTS_ES6_VALUE)
|
||||
.put(AbstractTypeScriptClientCodegen.NULL_SAFE_ADDITIONAL_PROPS, NULL_SAFE_ADDITIONAL_PROPS_VALUE)
|
||||
.put(CodegenConstants.ENUM_NAME_SUFFIX, ENUM_NAME_SUFFIX)
|
||||
.put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE)
|
||||
.put(CodegenConstants.ENUM_PROPERTY_NAMING, ENUM_PROPERTY_NAMING_VALUE)
|
||||
|
||||
@@ -20,6 +20,7 @@ package org.openapitools.codegen.options;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.openapitools.codegen.CodegenConstants;
|
||||
import org.openapitools.codegen.languages.TypeScriptAureliaClientCodegen;
|
||||
import org.openapitools.codegen.languages.AbstractTypeScriptClientCodegen;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@@ -28,6 +29,7 @@ public class TypeScriptAureliaClientOptionsProvider implements OptionsProvider {
|
||||
public static final String SORT_MODEL_PROPERTIES_VALUE = "false";
|
||||
public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true";
|
||||
public static final Boolean SUPPORTS_ES6_VALUE = false;
|
||||
public static final Boolean NULL_SAFE_ADDITIONAL_PROPS_VALUE = false;
|
||||
public static final String ENUM_NAME_SUFFIX = "Enum";
|
||||
public static final String ENUM_PROPERTY_NAMING_VALUE = "PascalCase";
|
||||
public static final String MODEL_PROPERTY_NAMING_VALUE = "camelCase";
|
||||
@@ -50,6 +52,7 @@ public class TypeScriptAureliaClientOptionsProvider implements OptionsProvider {
|
||||
.put(CodegenConstants.ENUM_PROPERTY_NAMING, ENUM_PROPERTY_NAMING_VALUE)
|
||||
.put(CodegenConstants.MODEL_PROPERTY_NAMING, MODEL_PROPERTY_NAMING_VALUE)
|
||||
.put(CodegenConstants.SUPPORTS_ES6, String.valueOf(SUPPORTS_ES6_VALUE))
|
||||
.put(AbstractTypeScriptClientCodegen.NULL_SAFE_ADDITIONAL_PROPS, String.valueOf(NULL_SAFE_ADDITIONAL_PROPS_VALUE))
|
||||
.put(CodegenConstants.ENUM_NAME_SUFFIX, ENUM_NAME_SUFFIX)
|
||||
.put(TypeScriptAureliaClientCodegen.NPM_NAME, NPM_NAME)
|
||||
.put(TypeScriptAureliaClientCodegen.NPM_VERSION, NPM_VERSION)
|
||||
|
||||
@@ -20,6 +20,7 @@ package org.openapitools.codegen.options;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.openapitools.codegen.CodegenConstants;
|
||||
import org.openapitools.codegen.languages.TypeScriptFetchClientCodegen;
|
||||
import org.openapitools.codegen.languages.AbstractTypeScriptClientCodegen;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@@ -28,6 +29,7 @@ public class TypeScriptFetchClientOptionsProvider implements OptionsProvider {
|
||||
public static final String SORT_MODEL_PROPERTIES_VALUE = "false";
|
||||
public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true";
|
||||
public static final Boolean SUPPORTS_ES6_VALUE = false;
|
||||
public static final Boolean NULL_SAFE_ADDITIONAL_PROPS_VALUE = false;
|
||||
public static final String ENUM_NAME_SUFFIX = "Enum";
|
||||
public static final String MODEL_PROPERTY_NAMING_VALUE = "camelCase";
|
||||
public static final String ENUM_PROPERTY_NAMING_VALUE = "PascalCase";
|
||||
@@ -52,6 +54,7 @@ public class TypeScriptFetchClientOptionsProvider implements OptionsProvider {
|
||||
.put(CodegenConstants.ENUM_PROPERTY_NAMING, ENUM_PROPERTY_NAMING_VALUE)
|
||||
.put(CodegenConstants.MODEL_PROPERTY_NAMING, MODEL_PROPERTY_NAMING_VALUE)
|
||||
.put(CodegenConstants.SUPPORTS_ES6, String.valueOf(SUPPORTS_ES6_VALUE))
|
||||
.put(AbstractTypeScriptClientCodegen.NULL_SAFE_ADDITIONAL_PROPS, String.valueOf(NULL_SAFE_ADDITIONAL_PROPS_VALUE))
|
||||
.put(CodegenConstants.ENUM_NAME_SUFFIX, ENUM_NAME_SUFFIX)
|
||||
.put(TypeScriptFetchClientCodegen.NPM_NAME, NMP_NAME)
|
||||
.put(TypeScriptFetchClientCodegen.NPM_VERSION, NMP_VERSION)
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.openapitools.codegen.options;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.openapitools.codegen.CodegenConstants;
|
||||
import org.openapitools.codegen.languages.AbstractTypeScriptClientCodegen;
|
||||
import org.openapitools.codegen.languages.TypeScriptAngularClientCodegen;
|
||||
|
||||
import java.util.Map;
|
||||
@@ -26,6 +27,7 @@ import java.util.Map;
|
||||
|
||||
public class TypeScriptNodeClientOptionsProvider implements OptionsProvider {
|
||||
public static final String SUPPORTS_ES6_VALUE = "false";
|
||||
public static final String NULL_SAFE_ADDITIONAL_PROPS_VALUE = "false";
|
||||
public static final String ENUM_NAME_SUFFIX = "Enum";
|
||||
public static final String SORT_PARAMS_VALUE = "false";
|
||||
public static final String SORT_MODEL_PROPERTIES_VALUE = "false";
|
||||
@@ -50,6 +52,7 @@ public class TypeScriptNodeClientOptionsProvider implements OptionsProvider {
|
||||
return builder.put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, SORT_PARAMS_VALUE)
|
||||
.put(CodegenConstants.SORT_MODEL_PROPERTIES_BY_REQUIRED_FLAG, SORT_MODEL_PROPERTIES_VALUE)
|
||||
.put(CodegenConstants.SUPPORTS_ES6, SUPPORTS_ES6_VALUE)
|
||||
.put(AbstractTypeScriptClientCodegen.NULL_SAFE_ADDITIONAL_PROPS, NULL_SAFE_ADDITIONAL_PROPS_VALUE)
|
||||
.put(CodegenConstants.ENUM_NAME_SUFFIX, ENUM_NAME_SUFFIX)
|
||||
.put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE)
|
||||
.put(CodegenConstants.ENUM_PROPERTY_NAMING, ENUM_PROPERTY_NAMING_VALUE)
|
||||
|
||||
@@ -294,4 +294,24 @@ public class TypeScriptFetchModelTest {
|
||||
|
||||
}
|
||||
|
||||
@Test(description = "Add null safe additional property indexer when enabled")
|
||||
public void testNullSafeAdditionalProps() {
|
||||
final Schema model = new Schema()
|
||||
.additionalProperties(new StringSchema());
|
||||
final DefaultCodegen codegen = new TypeScriptFetchClientCodegen();
|
||||
codegen.additionalProperties().put("nullSafeAdditionalProps", true);
|
||||
codegen.processOpts();
|
||||
|
||||
Assert.assertEquals(codegen.getTypeDeclaration(model), "{ [key: string]: string | undefined; }");
|
||||
}
|
||||
|
||||
@Test(description = "Don't add null safe additional property indexer by default")
|
||||
public void testWithoutNullSafeAdditionalProps() {
|
||||
final Schema model = new Schema()
|
||||
.additionalProperties(new StringSchema());
|
||||
final DefaultCodegen codegen = new TypeScriptFetchClientCodegen();
|
||||
codegen.processOpts();
|
||||
|
||||
Assert.assertEquals(codegen.getTypeDeclaration(model), "{ [key: string]: string; }");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user