Skip to content

Commit d8d2474

Browse files
committed
Update oneOf generator
1 parent a3c2ff4 commit d8d2474

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

.generator/src/generator/templates/modelOneOf.j2

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,23 @@ public class {{ name }} extends AbstractOpenApiSchema {
139139
super("oneOf", Boolean.{{ "TRUE" if model.nullable else "FALSE" }});
140140
}
141141

142+
{%- set constructor_sigs = [] %}
142143
{%- for oneOf in model.oneOf %}
143-
public {{ name }}({{ get_type(oneOf) }} o) {
144+
{%- set param_type = get_type(oneOf) %}
145+
{%- set unparam_type = param_type|un_parameterize_type %}
146+
{%- if unparam_type in constructor_sigs %}
147+
public static {{ name }} from{{ param_type.replace('<', '').replace('>', '').replace(' ', '').replace(',', '') }}({{ param_type }} o) {
148+
{{ name }} instance = new {{ name }}();
149+
instance.setActualInstance(o);
150+
return instance;
151+
}
152+
{%- else %}
153+
{%- set _ = constructor_sigs.append(unparam_type) %}
154+
public {{ name }}({{ param_type }} o) {
144155
super("oneOf", Boolean.{{ "TRUE" if model.nullable else "FALSE" }});
145156
setActualInstance(o);
146157
}
158+
{%- endif %}
147159
{%- endfor %}
148160

149161
static {
@@ -204,9 +216,16 @@ public class {{ name }} extends AbstractOpenApiSchema {
204216
return super.getActualInstance();
205217
}
206218

219+
{%- set method_names = [] %}
207220
{%- for oneOf in model.oneOf %}
208221
{%- set dataType = get_type(oneOf) %}
209222
{%- set unParameterizedDataType = get_type(oneOf)|un_parameterize_type %}
223+
{%- set method_name = "get" + unParameterizedDataType %}
224+
{%- if method_name in method_names %}
225+
{%- set safe_type = dataType.replace('<', '').replace('>', '').replace(' ', '').replace(',', '') %}
226+
{%- set method_name = "get" + safe_type %}
227+
{%- endif %}
228+
{%- set _ = method_names.append(method_name) %}
210229

211230
/**
212231
* Get the actual instance of `{{ dataType|escape_html }}`. If the actual instance is not `{{ dataType|escape_html }}`,
@@ -215,7 +234,7 @@ public class {{ name }} extends AbstractOpenApiSchema {
215234
* @return The actual instance of `{{ dataType|escape_html }}`
216235
* @throws ClassCastException if the instance is not `{{ dataType|escape_html }}`
217236
*/
218-
public {{ dataType }} get{{ unParameterizedDataType }}() throws ClassCastException {
237+
public {{ dataType }} {{ method_name }}() throws ClassCastException {
219238
return ({{ dataType }})super.getActualInstance();
220239
}
221240

0 commit comments

Comments
 (0)