Skip to content

Commit d2a721e

Browse files
committed
Ensure generation of properties that only have additionalProps + propertyNames enum
1 parent f26d0fe commit d2a721e

4 files changed

Lines changed: 15 additions & 5 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,9 @@ private boolean isModelNeeded(Schema schema, Set<Schema> visitedSchemas) {
282282
return true;
283283
}
284284
}
285+
if (ModelUtils.hasEnumPropertyNames(schema)) {
286+
return true;
287+
}
285288

286289
return false;
287290
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ public String getTypeDeclaration(Schema p) {
662662
if (Boolean.TRUE.equals(inner.getNullable())) {
663663
nullSafeSuffix += " | null";
664664
}
665-
return "{ [key: string]: " + getTypeDeclaration(unaliasSchema(inner)) + nullSafeSuffix + "; }";
665+
return "Record<string, " + getTypeDeclaration(unaliasSchema(inner)) + nullSafeSuffix + ">";
666666
} else if (ModelUtils.isFileSchema(p)) {
667667
return "File";
668668
} else if (ModelUtils.isBinarySchema(p)) {

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ public static boolean isFreeFormObject(Schema schema, OpenAPI openAPI) {
889889
if (schema.getAdditionalProperties() instanceof Boolean && (Boolean) schema.getAdditionalProperties()) {
890890
return true;
891891
} else if (schema.getAdditionalProperties() instanceof JsonSchema) {
892-
return true;
892+
return !ModelUtils.hasEnumPropertyNames(schema);
893893
} else if (schema.getTypes() != null) {
894894
if (schema.getTypes().size() == 1) { // types = [object]
895895
return SchemaTypeUtil.OBJECT_TYPE.equals(schema.getTypes().iterator().next());
@@ -937,7 +937,7 @@ public static boolean isFreeFormObject(Schema schema, OpenAPI openAPI) {
937937
return objSchema.getProperties() == null || objSchema.getProperties().isEmpty();
938938
} else if (addlProps instanceof Schema) {
939939
// additionalProperties defined as {}
940-
return addlProps.getType() == null && addlProps.get$ref() == null && (addlProps.getProperties() == null || addlProps.getProperties().isEmpty());
940+
return addlProps.getType() == null && addlProps.get$ref() == null && (addlProps.getProperties() == null || addlProps.getProperties().isEmpty()) && !ModelUtils.hasEnumPropertyNames(schema);
941941
}
942942
}
943943
}
@@ -963,7 +963,7 @@ public static boolean shouldGenerateMapModel(Schema schema) {
963963
// A composed schema (allOf, oneOf, anyOf) is considered a Map schema if the additionalproperties attribute is set
964964
// for that composed schema. However, in the case of a composed schema, the properties are defined or referenced
965965
// in the inner schemas, and the outer schema does not have properties.
966-
return ModelUtils.isGenerateAliasAsModel(schema) || ModelUtils.isComposedSchema(schema) || !(schema.getProperties() == null || schema.getProperties().isEmpty());
966+
return ModelUtils.isGenerateAliasAsModel(schema) || ModelUtils.isComposedSchema(schema) || !(schema.getProperties() == null || schema.getProperties().isEmpty()) || ModelUtils.hasEnumPropertyNames(schema);
967967
}
968968

969969
public static boolean shouldGenerateArrayModel(Schema schema) {
@@ -2158,6 +2158,13 @@ public static boolean hasAnyOf(Schema schema) {
21582158
return false;
21592159
}
21602160

2161+
public static boolean hasEnumPropertyNames(Schema schema) {
2162+
if (schema == null || schema.getPropertyNames() == null) {
2163+
return false;
2164+
}
2165+
return !schema.getPropertyNames().getEnum().isEmpty();
2166+
}
2167+
21612168
/**
21622169
* Returns schema type.
21632170
* For 3.1 spec, return the first one.

modules/openapi-generator/src/main/resources/typescript-angular/modelGenericAdditionalProperties.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[key: string]: {{additionalPropertiesType}}{{#hasVars}} | any{{/hasVars}};
44
{{#nameOnlyVars}}
5-
{{.}}?: {{additionalPropertiesType}};
5+
{{.}}?: {{additionalPropertiesType}};
66
{{/nameOnlyVars}}
77

88
{{/additionalPropertiesType}}

0 commit comments

Comments
 (0)