Skip to content

Commit 057647c

Browse files
authored
Do not use cached properties for additionalProperties (#7955)
* Fixes additionalProperties, do not use cached properties for additonalProperties * Regnerates samples
1 parent 36aba26 commit 057647c

9 files changed

Lines changed: 172 additions & 174 deletions

File tree

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

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,26 +2573,17 @@ public int compare(CodegenProperty one, CodegenProperty another) {
25732573
}
25742574

25752575
// process 'additionalProperties'
2576-
if (schema.getAdditionalProperties() == null) {
2577-
if (disallowAdditionalPropertiesIfNotPresent) {
2578-
m.isAdditionalPropertiesTrue = false;
2579-
} else {
2580-
m.isAdditionalPropertiesTrue = true;
2581-
CodegenProperty cp = fromProperty("", new Schema());
2582-
m.setAdditionalProperties(cp);
2583-
}
2584-
} else if (schema.getAdditionalProperties() instanceof Boolean) {
2585-
if (Boolean.TRUE.equals(schema.getAdditionalProperties())) {
2586-
m.isAdditionalPropertiesTrue = true;
2587-
CodegenProperty cp = fromProperty("", new Schema());
2588-
m.setAdditionalProperties(cp);
2589-
} else {
2590-
m.isAdditionalPropertiesTrue = false;
2591-
}
2576+
setAddProps(schema, m);
2577+
// additionalProperties == True means that empty object will be used
2578+
// per the openapi spec, if additionalProperties is omitted it is defaulted to empty object
2579+
// so if we do that, set isAdditionalPropertiesTrue to True
2580+
// if an explicit schema is passed in to additionalProperties, set isAdditionalPropertiesTrue to False
2581+
if (schema.getAdditionalProperties() == null && !disallowAdditionalPropertiesIfNotPresent) {
2582+
m.isAdditionalPropertiesTrue = true;
2583+
} else if (schema.getAdditionalProperties() instanceof Boolean && Boolean.TRUE.equals(schema.getAdditionalProperties())) {
2584+
m.isAdditionalPropertiesTrue = true;
25922585
} else {
25932586
m.isAdditionalPropertiesTrue = false;
2594-
CodegenProperty cp = fromProperty("", (Schema) schema.getAdditionalProperties());
2595-
m.setAdditionalProperties(cp);
25962587
}
25972588

25982589
// post process model properties
@@ -3037,6 +3028,7 @@ public String getterAndSetterCapitalize(String name) {
30373028
* the (String name, Schema p) arguments.
30383029
* Any subsequent processing of the CodegenModel return value must be idempotent
30393030
* for a given (String name, Schema schema).
3031+
* Pass in null for the name to not use the schemaCodegenPropertyCache
30403032
*
30413033
* @param name name of the property
30423034
* @param p OAS property schema
@@ -3048,11 +3040,16 @@ public CodegenProperty fromProperty(String name, Schema p) {
30483040
return null;
30493041
}
30503042
LOGGER.debug("debugging fromProperty for " + name + " : " + p);
3043+
boolean nullName = (name == null);
30513044
NamedSchema ns = new NamedSchema(name, p);
3052-
CodegenProperty cpc = schemaCodegenPropertyCache.get(ns);
3053-
if (cpc != null) {
3054-
LOGGER.debug("Cached fromProperty for " + name + " : " + p.getName());
3055-
return cpc;
3045+
if (!nullName) {
3046+
CodegenProperty cpc = schemaCodegenPropertyCache.get(ns);
3047+
if (cpc != null) {
3048+
LOGGER.debug("Cached fromProperty for " + name + " : " + p.getName());
3049+
return cpc;
3050+
}
3051+
} else {
3052+
name = "";
30563053
}
30573054
// unalias schema
30583055
p = unaliasSchema(p, importMapping);
@@ -3376,7 +3373,9 @@ public CodegenProperty fromProperty(String name, Schema p) {
33763373

33773374
addVarsRequiredVarsAdditionaProps(p, property);
33783375
LOGGER.debug("debugging from property return: " + property);
3379-
schemaCodegenPropertyCache.put(ns, property);
3376+
if (!nullName) {
3377+
schemaCodegenPropertyCache.put(ns, property);
3378+
}
33803379
return property;
33813380
}
33823381

@@ -6162,6 +6161,25 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
61626161
return codegenParameter;
61636162
}
61646163

6164+
private void setAddProps(Schema schema, IJsonSchemaValidationProperties property){
6165+
Schema usedSchema = new Schema();
6166+
if (schema.getAdditionalProperties() == null) {
6167+
if (!disallowAdditionalPropertiesIfNotPresent) {
6168+
CodegenProperty cp = fromProperty(null, usedSchema);
6169+
property.setAdditionalProperties(cp);
6170+
}
6171+
} else if (schema.getAdditionalProperties() instanceof Boolean) {
6172+
if (Boolean.TRUE.equals(schema.getAdditionalProperties())) {
6173+
CodegenProperty cp = fromProperty(null, usedSchema);
6174+
property.setAdditionalProperties(cp);
6175+
}
6176+
} else {
6177+
usedSchema = (Schema) schema.getAdditionalProperties();
6178+
CodegenProperty cp = fromProperty(null, usedSchema);
6179+
property.setAdditionalProperties(cp);
6180+
}
6181+
}
6182+
61656183
private void addVarsRequiredVarsAdditionaProps(Schema schema, IJsonSchemaValidationProperties property){
61666184
if (!"object".equals(schema.getType())) {
61676185
return;
@@ -6178,20 +6196,7 @@ private void addVarsRequiredVarsAdditionaProps(Schema schema, IJsonSchemaValidat
61786196
.filter(p -> Boolean.TRUE.equals(p.required)).collect(Collectors.toList());
61796197
property.setRequiredVars(requireCpVars);
61806198
}
6181-
if (schema.getAdditionalProperties() == null) {
6182-
if (!disallowAdditionalPropertiesIfNotPresent) {
6183-
CodegenProperty cp = fromProperty("", new Schema());
6184-
property.setAdditionalProperties(cp);
6185-
}
6186-
} else if (schema.getAdditionalProperties() instanceof Boolean) {
6187-
if (Boolean.TRUE.equals(schema.getAdditionalProperties())) {
6188-
CodegenProperty cp = fromProperty("", new Schema());
6189-
property.setAdditionalProperties(cp);
6190-
}
6191-
} else {
6192-
CodegenProperty cp = fromProperty("", (Schema) schema.getAdditionalProperties());
6193-
property.setAdditionalProperties(cp);
6194-
}
6199+
setAddProps(schema, property);
61956200
return;
61966201
}
61976202

samples/client/petstore/typescript-fetch/builds/default-v3.0/models/EnumArrays.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@ export interface EnumArrays {
3333
arrayEnum?: Array<EnumArraysArrayEnumEnum>;
3434
}
3535

36+
/**
37+
* @export
38+
* @enum {string}
39+
*/
40+
export enum EnumArraysJustSymbolEnum {
41+
GreaterThanOrEqualTo = '>=',
42+
Dollar = '$'
43+
}/**
44+
* @export
45+
* @enum {string}
46+
*/
47+
export enum EnumArraysArrayEnumEnum {
48+
Fish = 'fish',
49+
Crab = 'crab'
50+
}
51+
3652
export function EnumArraysFromJSON(json: any): EnumArrays {
3753
return EnumArraysFromJSONTyped(json, false);
3854
}
@@ -62,21 +78,4 @@ export function EnumArraysToJSON(value?: EnumArrays | null): any {
6278
};
6379
}
6480

65-
/**
66-
* @export
67-
* @enum {string}
68-
*/
69-
export enum EnumArraysJustSymbolEnum {
70-
GreaterThanOrEqualTo = '>=',
71-
Dollar = '$'
72-
}
73-
/**
74-
* @export
75-
* @enum {string}
76-
*/
77-
export enum EnumArraysArrayEnumEnum {
78-
Fish = 'fish',
79-
Crab = 'crab'
80-
}
81-
8281

samples/client/petstore/typescript-fetch/builds/default-v3.0/models/EnumTest.ts

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,38 @@ export interface EnumTest {
8888
outerEnumIntegerDefaultValue?: OuterEnumIntegerDefaultValue;
8989
}
9090

91+
/**
92+
* @export
93+
* @enum {string}
94+
*/
95+
export enum EnumTestEnumStringEnum {
96+
Upper = 'UPPER',
97+
Lower = 'lower',
98+
Empty = ''
99+
}/**
100+
* @export
101+
* @enum {string}
102+
*/
103+
export enum EnumTestEnumStringRequiredEnum {
104+
Upper = 'UPPER',
105+
Lower = 'lower',
106+
Empty = ''
107+
}/**
108+
* @export
109+
* @enum {string}
110+
*/
111+
export enum EnumTestEnumIntegerEnum {
112+
NUMBER_1 = 1,
113+
NUMBER_MINUS_1 = -1
114+
}/**
115+
* @export
116+
* @enum {string}
117+
*/
118+
export enum EnumTestEnumNumberEnum {
119+
NUMBER_1_DOT_1 = 1.1,
120+
NUMBER_MINUS_1_DOT_2 = -1.2
121+
}
122+
91123
export function EnumTestFromJSON(json: any): EnumTest {
92124
return EnumTestFromJSONTyped(json, false);
93125
}
@@ -129,39 +161,4 @@ export function EnumTestToJSON(value?: EnumTest | null): any {
129161
};
130162
}
131163

132-
/**
133-
* @export
134-
* @enum {string}
135-
*/
136-
export enum EnumTestEnumStringEnum {
137-
Upper = 'UPPER',
138-
Lower = 'lower',
139-
Empty = ''
140-
}
141-
/**
142-
* @export
143-
* @enum {string}
144-
*/
145-
export enum EnumTestEnumStringRequiredEnum {
146-
Upper = 'UPPER',
147-
Lower = 'lower',
148-
Empty = ''
149-
}
150-
/**
151-
* @export
152-
* @enum {string}
153-
*/
154-
export enum EnumTestEnumIntegerEnum {
155-
NUMBER_1 = 1,
156-
NUMBER_MINUS_1 = -1
157-
}
158-
/**
159-
* @export
160-
* @enum {string}
161-
*/
162-
export enum EnumTestEnumNumberEnum {
163-
NUMBER_1_DOT_1 = 1.1,
164-
NUMBER_MINUS_1_DOT_2 = -1.2
165-
}
166-
167164

samples/client/petstore/typescript-fetch/builds/default-v3.0/models/InlineObject2.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ export interface InlineObject2 {
3333
enumFormString?: InlineObject2EnumFormStringEnum;
3434
}
3535

36+
/**
37+
* @export
38+
* @enum {string}
39+
*/
40+
export enum InlineObject2EnumFormStringArrayEnum {
41+
GreaterThan = '>',
42+
Dollar = '$'
43+
}/**
44+
* @export
45+
* @enum {string}
46+
*/
47+
export enum InlineObject2EnumFormStringEnum {
48+
Abc = '_abc',
49+
Efg = '-efg',
50+
Xyz = '(xyz)'
51+
}
52+
3653
export function InlineObject2FromJSON(json: any): InlineObject2 {
3754
return InlineObject2FromJSONTyped(json, false);
3855
}
@@ -62,22 +79,4 @@ export function InlineObject2ToJSON(value?: InlineObject2 | null): any {
6279
};
6380
}
6481

65-
/**
66-
* @export
67-
* @enum {string}
68-
*/
69-
export enum InlineObject2EnumFormStringArrayEnum {
70-
GreaterThan = '>',
71-
Dollar = '$'
72-
}
73-
/**
74-
* @export
75-
* @enum {string}
76-
*/
77-
export enum InlineObject2EnumFormStringEnum {
78-
Abc = '_abc',
79-
Efg = '-efg',
80-
Xyz = '(xyz)'
81-
}
82-
8382

samples/client/petstore/typescript-fetch/builds/default-v3.0/models/MapTest.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ export interface MapTest {
4545
indirectMap?: { [key: string]: boolean; };
4646
}
4747

48+
/**
49+
* @export
50+
* @enum {string}
51+
*/
52+
export enum MapTestMapOfEnumStringEnum {
53+
Upper = 'UPPER',
54+
Lower = 'lower'
55+
}
56+
4857
export function MapTestFromJSON(json: any): MapTest {
4958
return MapTestFromJSONTyped(json, false);
5059
}
@@ -78,13 +87,4 @@ export function MapTestToJSON(value?: MapTest | null): any {
7887
};
7988
}
8089

81-
/**
82-
* @export
83-
* @enum {string}
84-
*/
85-
export enum MapTestMapOfEnumStringEnum {
86-
Upper = 'UPPER',
87-
Lower = 'lower'
88-
}
89-
9090

samples/client/petstore/typescript-fetch/builds/default-v3.0/models/Order.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ export interface Order {
5757
complete?: boolean;
5858
}
5959

60+
/**
61+
* @export
62+
* @enum {string}
63+
*/
64+
export enum OrderStatusEnum {
65+
Placed = 'placed',
66+
Approved = 'approved',
67+
Delivered = 'delivered'
68+
}
69+
6070
export function OrderFromJSON(json: any): Order {
6171
return OrderFromJSONTyped(json, false);
6272
}
@@ -94,14 +104,4 @@ export function OrderToJSON(value?: Order | null): any {
94104
};
95105
}
96106

97-
/**
98-
* @export
99-
* @enum {string}
100-
*/
101-
export enum OrderStatusEnum {
102-
Placed = 'placed',
103-
Approved = 'approved',
104-
Delivered = 'delivered'
105-
}
106-
107107

samples/client/petstore/typescript-fetch/builds/default-v3.0/models/Pet.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ export interface Pet {
6868
status?: PetStatusEnum;
6969
}
7070

71+
/**
72+
* @export
73+
* @enum {string}
74+
*/
75+
export enum PetStatusEnum {
76+
Available = 'available',
77+
Pending = 'pending',
78+
Sold = 'sold'
79+
}
80+
7181
export function PetFromJSON(json: any): Pet {
7282
return PetFromJSONTyped(json, false);
7383
}
@@ -105,14 +115,4 @@ export function PetToJSON(value?: Pet | null): any {
105115
};
106116
}
107117

108-
/**
109-
* @export
110-
* @enum {string}
111-
*/
112-
export enum PetStatusEnum {
113-
Available = 'available',
114-
Pending = 'pending',
115-
Sold = 'sold'
116-
}
117-
118118

0 commit comments

Comments
 (0)