Skip to content

Commit a729cb4

Browse files
authored
Add support for isFreeFormObject flag (#16127)
* correctly set the free form object flag * update * better code format
1 parent bfcd646 commit a729cb4

File tree

7 files changed

+61
-9
lines changed

7 files changed

+61
-9
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
6666
public String defaultValue;
6767
public String arrayModelType;
6868
public boolean isAlias; // Is this effectively an alias of another simple type
69-
public boolean isString, isInteger, isLong, isNumber, isNumeric, isFloat, isDouble, isDate, isDateTime, isDecimal, isShort, isUnboundedInteger, isPrimitiveType, isBoolean;
69+
public boolean isString, isInteger, isLong, isNumber, isNumeric, isFloat, isDouble, isDate, isDateTime,
70+
isDecimal, isShort, isUnboundedInteger, isPrimitiveType, isBoolean, isFreeFormObject;
7071
private boolean additionalPropertiesIsAnyType;
7172
public List<CodegenProperty> vars = new ArrayList<>(); // all properties (without parent's properties)
7273
public List<CodegenProperty> allVars = new ArrayList<>(); // all properties (with parent's properties)
@@ -961,6 +962,16 @@ public void setIsAnyType(boolean isAnyType) {
961962
this.isAnyType = isAnyType;
962963
}
963964

965+
@Override
966+
public boolean getIsFreeFormObject() {
967+
return isFreeFormObject;
968+
}
969+
970+
@Override
971+
public void setIsFreeFormObject(boolean isFreeFormObject) {
972+
this.isFreeFormObject = isFreeFormObject;
973+
}
974+
964975
public boolean getIsUuid() { return isUuid; }
965976

966977
public void setIsUuid(boolean isUuid) { this.isUuid = isUuid; }

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,16 @@ public void setIsAnyType(boolean isAnyType) {
914914
this.isAnyType = isAnyType;
915915
}
916916

917+
@Override
918+
public boolean getIsFreeFormObject() {
919+
return isFreeFormObject;
920+
}
921+
922+
@Override
923+
public void setIsFreeFormObject(boolean isFreeFormObject) {
924+
this.isFreeFormObject = isFreeFormObject;
925+
}
926+
917927
@Override
918928
public void setComposedSchemas(CodegenComposedSchemas composedSchemas) {
919929
this.composedSchemas = composedSchemas;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,16 @@ public void setIsAnyType(boolean isAnyType) {
989989
this.isAnyType = isAnyType;
990990
}
991991

992+
@Override
993+
public boolean getIsFreeFormObject() {
994+
return isFreeFormObject;
995+
}
996+
997+
@Override
998+
public void setIsFreeFormObject(boolean isFreeFormObject) {
999+
this.isFreeFormObject = isFreeFormObject;
1000+
}
1001+
9921002
@Override
9931003
public boolean getHasMultipleTypes() {
9941004
return hasMultipleTypes;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,16 @@ public void setIsAnyType(boolean isAnyType) {
734734
this.isAnyType = isAnyType;
735735
}
736736

737+
@Override
738+
public boolean getIsFreeFormObject() {
739+
return isFreeFormObject;
740+
}
741+
742+
@Override
743+
public void setIsFreeFormObject(boolean isFreeFormObject) {
744+
this.isFreeFormObject = isFreeFormObject;
745+
}
746+
737747
@Override
738748
public void setComposedSchemas(CodegenComposedSchemas composedSchemas) {
739749
this.composedSchemas = composedSchemas;

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ public interface IJsonSchemaValidationProperties {
189189

190190
void setIsAnyType(boolean isAnyType);
191191

192+
boolean getIsFreeFormObject();
193+
194+
void setIsFreeFormObject(boolean isFreeFormObject);
195+
192196
String getRef();
193197

194198
void setRef(String ref);
@@ -223,11 +227,12 @@ public interface IJsonSchemaValidationProperties {
223227
* Syncs all the schema's type properties into the IJsonSchemaValidationProperties instance
224228
* for now this only supports types without format information
225229
* TODO: in the future move the format handling in here too
230+
*
226231
* @param p the schema which contains the type info
227232
*/
228233
default void setTypeProperties(Schema p) {
229234
if (ModelUtils.isModelWithPropertiesOnly(p)) {
230-
setIsModel(true);
235+
setIsModel(true);
231236
} else if (ModelUtils.isArraySchema(p)) {
232237
setIsArray(true);
233238
} else if (ModelUtils.isFileSchema(p) && !ModelUtils.isStringSchema(p)) {
@@ -249,7 +254,7 @@ default void setTypeProperties(Schema p) {
249254
} else if (ModelUtils.isEmailSchema(p)) {
250255
;
251256
} else if (ModelUtils.isPasswordSchema(p)) {
252-
;
257+
;
253258
} else if (ModelUtils.isDateSchema(p)) {
254259
;
255260
} else if (ModelUtils.isDateTimeSchema(p)) {
@@ -279,6 +284,10 @@ default void setTypeProperties(Schema p) {
279284
setIsNull(true);
280285
} else if (ModelUtils.isAnyType(p)) {
281286
setIsAnyType(true);
287+
} else if (ModelUtils.isFreeFormObject(p)) {
288+
setIsFreeFormObject(true);
289+
// TODO: remove below later after updating generators to properly use isFreeFormObject
290+
setIsMap(true);
282291
} else if (ModelUtils.isTypeObjectSchema(p)) {
283292
setIsMap(true);
284293
}
@@ -289,21 +298,21 @@ default void setTypeProperties(Schema p) {
289298
*/
290299
default String getBaseType() {
291300
return null;
292-
};
301+
}
293302

294303
/**
295304
* @return complex type that can contain type parameters - like {@code List<Items>} for Java
296305
*/
297306
default String getComplexType() {
298307
return getBaseType();
299-
};
308+
}
300309

301310
/**
302311
* Recursively collect all necessary imports to include so that the type may be resolved.
303312
*
304313
* @param importContainerType whether or not to include the container types in the returned imports.
305-
* @param importBaseType whether or not to include the base types in the returned imports.
306-
* @param featureSet the generator feature set, used to determine if composed schemas should be added
314+
* @param importBaseType whether or not to include the base types in the returned imports.
315+
* @param featureSet the generator feature set, used to determine if composed schemas should be added
307316
* @return all of the imports
308317
*/
309318
default Set<String> getImports(boolean importContainerType, boolean importBaseType, FeatureSet featureSet) {

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,14 +872,15 @@ public void testAnyType() {
872872
// map
873873
// Should allow in any type including map, https://github.com/swagger-api/swagger-parser/issues/1603
874874
final CodegenProperty cp4 = cm2.vars.get(3);
875-
Assert.assertEquals(cp4.baseName, "map_any_value");
875+
Assert.assertEquals(cp4.baseName, "map_free_form_object");
876876
Assert.assertEquals(cp4.dataType, "Map<String, Object>");
877877
Assert.assertFalse(cp4.required);
878878
Assert.assertTrue(cp4.isPrimitiveType);
879879
Assert.assertTrue(cp4.isContainer);
880880
Assert.assertTrue(cp4.isMap);
881881
Assert.assertTrue(cp4.isFreeFormObject);
882882
Assert.assertFalse(cp4.isAnyType);
883+
Assert.assertFalse(cp4.isModel);
883884

884885
// Should allow in any type including map, https://github.com/swagger-api/swagger-parser/issues/1603
885886
final CodegenProperty cp5 = cm2.vars.get(4);
@@ -891,6 +892,7 @@ public void testAnyType() {
891892
Assert.assertTrue(cp5.isMap);
892893
Assert.assertTrue(cp5.isFreeFormObject);
893894
Assert.assertFalse(cp5.isAnyType);
895+
Assert.assertFalse(cp5.isModel);
894896

895897
// Should allow in any type including map, https://github.com/swagger-api/swagger-parser/issues/1603
896898
final CodegenProperty cp6 = cm2.vars.get(5);

modules/openapi-generator/src/test/resources/3_0/any_type.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ components:
3939
any_value_nullable:
4040
nullable: true
4141
description: inline any value nullable
42-
map_any_value:
42+
map_free_form_object:
4343
additionalProperties: {}
4444
map_any_value_with_desc:
4545
additionalProperties:

0 commit comments

Comments
 (0)