-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathmodelSimple.j2
More file actions
376 lines (358 loc) · 15.2 KB
/
Copy pathmodelSimple.j2
File metadata and controls
376 lines (358 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/**
{{ model.description|docstring }}
{%- if model.deprecated %}
* @deprecated
{%- endif %}
*/
{%- if model.deprecated %}
@Deprecated
{%- endif %}
@JsonPropertyOrder({
{%- for attr, schema in model.get("properties", {}).items() %}
{{ name }}.JSON_PROPERTY_{{ attr|snake_case|upper }}{%- if loop.nextitem %},{%- endif %}
{%- endfor %}
})
{{ generated_annotation }}
public class {{ name }} {%- if model.get("x-generate-alias-as-model") %} extends {% if "items" in model %}ArrayList<{{ get_type(model["items"]) }}>{% else %}{{ get_type(model) }}{% endif %}{% endif %} {
@JsonIgnore
public boolean unparsed = false;
{%- for attr, schema in model.get("properties", {}).items() %}
{%- set attributeName = attr|attribute_name %}
{%- set variableName = attr|variable_name %}
{%- set dataType = get_type(schema) %}
{%- set isRequired = attr in model.get("required", []) %}
{%- set isNullable = schema.nullable %}
{%- set defaultValue = schema.get("default", None) %}
{%- set isAdditionalPropertiesContainer = schema.properties is not defined and schema.additionalProperties is defined and schema.additionalProperties is not false %}
public static final String JSON_PROPERTY_{{ attr|snake_case|upper }} = "{{ attr }}";
{%- if not isRequired and isNullable %}
{%- if "items" in schema or (isAdditionalPropertiesContainer) or (schema.type is not defined and "oneOf" not in schema) %}
private JsonNullable<{{ dataType }}> {{ variableName }} = JsonNullable.<{{ dataType }}>undefined();
{%- else %}
private JsonNullable<{{ dataType }}> {{ variableName }} = JsonNullable.<{{ dataType }}>{%- if defaultValue != None %}of({{ defaultValue|format_value(schema=schema, default_value=True, type_=dataType) }}){%- else %}undefined(){%- endif%};
{%- endif %}
{%- else %}
{%- if "items" in schema %}
private {{ dataType }} {{ variableName }}{%- if isRequired %} = new ArrayList<>(){%- else %} = null{%- endif %};
{%- elif (isAdditionalPropertiesContainer) or (not schema.get("type") and "oneOf" not in schema) %}
private {{ dataType }} {{ variableName }}{%- if isRequired %} = new {{ get_type(schema, render_new=True) }}(){%- else %} = null{%- endif %};
{%- else %}
private {{ dataType }} {{ variableName }}{%- if defaultValue != None %} = {{ defaultValue|format_value(schema=schema, default_value=True, type_=dataType) }}{%- endif %};
{%- endif %}
{%- endif %}
{# #}
{%- endfor %}
{%- set requiredAttr = model|get_required_attributes %}
{%- if requiredAttr %}
public {{ name }}() {}
@JsonCreator
public {{ name }}(
{%- for attr, schema in requiredAttr.items() %}
{%- set attributeName = attr|attribute_name %}
{%- set variableName = attr|variable_name %}
{%- set isRequired = attr in model.get("required", []) %}
{%- set isNullable = schema.nullable %}
{%- set dataType = get_type(schema) %}
@JsonProperty(required=true, value=JSON_PROPERTY_{{ attr|snake_case|upper }})
{%- if not isRequired and isNullable %}JsonNullable<{{ dataType }}>{%- else %}{{ dataType }}{%- endif %} {{ variableName }}{%- if loop.nextitem %},{%- endif %}
{%- endfor %}) {
{%- for attr, schema in requiredAttr.items() %}
{%- set attributeName = attr|attribute_name %}
{%- set variableName = attr|variable_name %}
{%- set isNullable = schema.nullable %}
this.{{ variableName }} = {{ variableName }};
{%- if isNullable %}
if ({{ variableName }} != null) {
{%- endif %}
{%- if schema.enum is defined %}
this.unparsed |= !{{ variableName }}.isValid();
{%- endif %}
{%- if (schema.properties is defined or schema.oneOf is defined) and not schema|is_primitive %}
this.unparsed |= {{ variableName }}.unparsed;
{%- endif %}
{%- if isNullable %}
}
{%- endif %}
{%- endfor %}
}
{%- endif %}
{%- for attr, schema in model.get("properties", {}).items() %}
{%- set attributeName = attr|attribute_name %}
{%- set variableName = attr|variable_name %}
{%- set isNullable = schema.nullable %}
{%- set dataType = get_type(schema) %}
{%- set isArray = "items" in schema %}
{%- set isRequired = attr in model.get("required", []) %}
{%- set defaultValue = schema.get("default", None) %}
{%- set isAdditionalPropertiesContainer = schema.properties is not defined and schema.additionalProperties is defined and schema.additionalProperties is not false %}
{%- if not schema.get("readOnly", False) %}
public {{ name }} {{ variableName }}({{ dataType }} {{ variableName }}) {
{%- if not isRequired and isNullable %}
this.{{ variableName }} = JsonNullable.<{{ dataType }}>of({{ variableName }});
{%- else %}
this.{{ variableName }} = {{ variableName }};
{%- if not isArray %}
{%- if isNullable %}
if ({{ variableName }} != null) {
{%- endif %}
{%- if schema.enum is defined %}
{%- if not schema|is_primitive %}
this.unparsed |= !{{ variableName }}.isValid();
{%- endif %}
{%- endif %}
{%- if schema|is_model and not schema|is_primitive %}
this.unparsed |= {{ variableName }}.unparsed;
{%- endif %}
{%- if isNullable %}
}
{%- endif %}
{%- else %}
{%- if schema.get("items")|is_model and not schema|is_primitive %}
{%- set itemsDataType = get_type(schema.get("items")) %}
for ({{ itemsDataType }} item : {{ variableName }}) {
{%- if schema.enum is defined %}
this.unparsed |= !item.isValid();
{%- endif %}
this.unparsed |= item.unparsed;
}
{%- endif %}
{%- endif %}
{%- endif %}
return this;
}
{%- if isArray %}
{%- set itemsDataType = get_type(schema.get("items")) %}
public {{ name }} add{{ variableName|upperfirst }}Item({{ itemsDataType }} {{ variableName }}Item) {
{%- if not isRequired and isNullable %}
if (this.{{ variableName }} == null || !this.{{ variableName }}.isPresent()) {
this.{{ variableName }} = JsonNullable.<{{ dataType }}>of(new ArrayList<>());
}
try {
this.{{ variableName }}.get().add({{ variableName }}Item);
} catch (java.util.NoSuchElementException e) {
// this can never happen, as we make sure above that the value is present
}
return this;
{%- else %}
{%- if not isRequired %}
if (this.{{ variableName }} == null) {
this.{{ variableName }} = new ArrayList<>();
}
{%- endif %}
this.{{ variableName }}.add({{ variableName }}Item);
{%- if schema.get("items", {}).enum is defined %}
{%- if not schema.get("items", {})|is_primitive %}
this.unparsed |= !{{ variableName }}Item.isValid();
{%- endif %}
{%- endif %}
{%- if schema.get("items", {})|is_model %}
{%- if not schema.get("items", {})|is_primitive %}
this.unparsed |= {{ variableName }}Item.unparsed;
{%- endif %}
{%- endif %}
return this;
{%- endif %}
}
{%- endif %}
{%- if isAdditionalPropertiesContainer %}
public {{ name }} put{{ variableName|upperfirst }}Item(String key, {{ get_type(schema.additionalProperties) }} {{ variableName }}Item) {
{%- if not isRequired and isNullable %}
if (this.{{ variableName }} == null || !this.{{ variableName }}.isPresent()) {
this.{{ variableName }} = JsonNullable.<{{ dataType }}>of(new HashMap<>());
}
try {
this.{{ variableName }}.get().put(key, {{ variableName }}Item);
} catch (java.util.NoSuchElementException e) {
// this can never happen, as we make sure above that the value is present
}
return this;
{%- else %}
{%- if not isRequired %}
if (this.{{ variableName }} == null) {
this.{{ variableName }} = new HashMap<>();
}
{%- endif %}
this.{{ variableName }}.put(key, {{ variableName }}Item);
return this;
{%- endif %}
}
{%- endif %}
{%- endif %}
/**
{{ schema.description|default("Get" ~ variableName)|docstring }}
{%- if schema.minimum is defined %}
* minimum: {{ schema.minimum }}
{%- endif %}
{%- if schema.maximum is defined %}
* maximum: {{ schema.maximum }}
{%- endif %}
* @return {{ variableName }}
{%- if schema.deprecated %}
* @deprecated
{%- endif %}
**/
{%- if schema.deprecated %}
@Deprecated
{%- endif %}
{%- if isRequired %}
{%- if isNullable %}
@jakarta.annotation.Nullable
{%- endif %}
{%- else %}
@jakarta.annotation.Nullable
{%- endif %}
{%- if not isRequired and isNullable %}
@JsonIgnore
{%- else %}
@JsonProperty(JSON_PROPERTY_{{ attr|snake_case|upper }})
@JsonInclude(
{%- if schema.additionalProperties is defined and schema.additionalProperties is not false %}{%- if schema.additionalProperties.nullable %}content = JsonInclude.Include.ALWAYS,{%- endif %}{%- endif %}
value = JsonInclude.Include.{%- if isRequired %}ALWAYS{%- else %}USE_DEFAULTS{%- endif %})
{%- endif %}
public {{ dataType }} get{{ attr|camel_case|upperfirst|escape_method_reserved_name }}() {
{%- if not isRequired and isNullable %}
{%- if schema.get("readOnly", False) %}
if ({{ variableName }} == null) {
{{ variableName }} = JsonNullable.<{{ dataType }}>{%- if defaultValue != None %}of({{ defaultValue|format_value(schema=schema, default_value=True, type_=dataType) }}){%- else %}undefined(){%- endif%};
}
{%- endif %}
return {{ variableName }}.orElse(null);
{%- else %}
return {{ variableName }};
{%- endif %}
}
{%- if schema.deprecated %}
@Deprecated
{%- endif %}
{%- if not isRequired and isNullable %}
@JsonProperty(JSON_PROPERTY_{{ attr|snake_case|upper }})
@JsonInclude(
{%- if schema.additionalProperties is defined and schema.additionalProperties is not false %}{%- if schema.additionalProperties.nullable %}content = JsonInclude.Include.ALWAYS,{%- endif %}{%- endif %}
value = JsonInclude.Include.{%- if isRequired %}ALWAYS{%- else %}USE_DEFAULTS{%- endif %})
public JsonNullable<{{ dataType }}> get{{ attr|camel_case|upperfirst }}_JsonNullable() {
return {{ variableName }};
}
@JsonProperty(JSON_PROPERTY_{{ attr|snake_case|upper }})
{%- if schema.get("readOnly", False) %}private{%- else %}public{%- endif %} void set{{ attr|camel_case|upperfirst }}_JsonNullable(JsonNullable<{{ dataType }}> {{ variableName }}) {
this.{{ variableName }} = {{ variableName }};
}
{%- endif %}
{%- if not schema.get("readOnly", False) %}
public void set{{ attr|camel_case|upperfirst|escape_method_reserved_name }}({{ dataType }} {{ variableName }}) {
{%- if schema.enum is defined %}
if (!{{ variableName }}.isValid()) {
this.unparsed = true;
}
{%- endif %}
{%- if not isRequired and isNullable %}
this.{{ variableName }} = JsonNullable.<{{ dataType }}>of({{ variableName }});
{%- else %}
this.{{ variableName }} = {{ variableName }};
{%- endif %}
{%- if model.get("x-keep-typed-in-additional-properties") and model.additionalProperties is not false %}
putAdditionalProperty(JSON_PROPERTY_{{ attr|snake_case|upper }}, {%- if not isRequired and isNullable %} this.{{ variableName }}.orElse(null){%- else %} {{ variableName }}{%- endif %});
{%- endif %}
}
{%- endif %}
{%- endfor %}
{%- if model.additionalProperties is not false %}
{%- set additionalPropertiesDataType = model.get("additionalProperties", {})|simple_type or "Object" %}
/**
* A container for additional, undeclared properties.
* This is a holder for any undeclared properties as specified with
* the 'additionalProperties' keyword in the OAS document.
*/
private Map<String, {{ additionalPropertiesDataType }}> additionalProperties;
/**
* Set the additional (undeclared) property with the specified name and value.
* If the property does not already exist, create it otherwise replace it.
*
* @param key The arbitrary key to set
* @param value The associated value
* @return {{ name }}
*/
@JsonAnySetter
public {{ name }} putAdditionalProperty(String key, {{ additionalPropertiesDataType }} value) {
if (this.additionalProperties == null) {
this.additionalProperties = new HashMap<String, {{ additionalPropertiesDataType }}>();
}
this.additionalProperties.put(key, value);
return this;
}
/**
* Return the additional (undeclared) property.
*
* @return The additional properties
*/
@JsonAnyGetter
public Map<String, {{ additionalPropertiesDataType }}> getAdditionalProperties() {
return additionalProperties;
}
/**
* Return the additional (undeclared) property with the specified name.
*
* @param key The arbitrary key to get
* @return The specific additional property for the given key
*/
public {{ additionalPropertiesDataType }} getAdditionalProperty(String key) {
if (this.additionalProperties == null) {
return null;
}
return this.additionalProperties.get(key);
}
{%- endif %}
/**
* Return true if this {{name}} object is equal to o.
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
{%- if model.properties is defined %}
{{ name }} {{ name|variable_name }} = ({{ name }}) o;
return {%- for attr, schema in model.get("properties").items() %} Objects.equals(this.{{ attr|variable_name }}, {{ name|variable_name }}.{{ attr|variable_name }}){%- if loop.nextitem or model.additionalProperties is not false %} &&{%- endif %}{%- endfor %}{% if model.additionalProperties is not false %} Objects.equals(this.additionalProperties, {{ name|variable_name }}.additionalProperties){% endif %}
{%- if model.get("x-generate-alias-as-model") %} &&
super.equals(o){%- endif %};
{%- else %}
return {%- if model.get("x-generate-alias-as-model") %} super.equals(o){%- else %} true{%-endif %};
{%- endif %}
}
@Override
public int hashCode() {
return Objects.hash({%- for attr, schema in model.get("properties", {}).items() %}{{ attr|variable_name }}{%- if loop.nextitem %}, {%- endif %}{%-endfor %}
{%- if model.additionalProperties is not false %}{%- if model.properties is defined %}, {%- endif %} additionalProperties{%- endif %}
{%- if model.get("x-generate-alias-as-model") %}{%- if model.properties is defined or model.additionalProperties is not false %}, {%- endif %}super.hashCode(){%- endif %});
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class {{ name }} {\n");
{%- if model.get("x-generate-alias-as-model") %}
sb.append(" ").append(toIndentedString(super.toString())).append("\n");
{%- endif %}
{%- for attr, schema in model.get("properties", {}).items() %}
sb.append(" {{ attr|variable_name }}: ").append(toIndentedString({{ attr|variable_name }})).append("\n");
{%- endfor %}
{%- if model.additionalProperties is not false %}
sb.append(" additionalProperties: ")
.append(toIndentedString(additionalProperties))
.append("\n");
{%- endif %}
sb.append('}');
return sb.toString();
}
/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}