Skip to content

Commit 2a85824

Browse files
authored
Fix oneOf deserialization for parameterized types (#3193)
1 parent 29a9e58 commit 2a85824

File tree

7 files changed

+45
-20
lines changed

7 files changed

+45
-20
lines changed

.generator/src/generator/cli.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def cli(specs, output):
5252
env.filters["docstring"] = formatter.docstring
5353
env.filters["inline_docstring"] = formatter.inline_docstring
5454
env.filters["un_parameterize_type"] = formatter.un_parameterize_type
55+
env.filters["is_parameterized_type"] = formatter.is_parameterized_type
5556

5657
env.globals["enumerate"] = enumerate
5758
env.globals["get_name"] = openapi.get_name

.generator/src/generator/formatter.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ def un_parameterize_type(type):
157157
return UN_PARAMETERIZE.sub("", type)
158158

159159

160+
def is_parameterized_type(type):
161+
"""Check if a type has generic parameters (e.g., List<String>)."""
162+
return '<' in type and '>' in type
163+
164+
160165
def format_value(value, quotes='"', schema=None, default_value=False, type_=None):
161166
if schema:
162167
if "enum" in schema and default_value:

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ public class {{ name }} extends AbstractOpenApiSchema {
7070
int match = 0;
7171
JsonToken token = tree.traverse(jp.getCodec()).nextToken();
7272
{%- for oneOf in model.oneOf %}
73-
{%- set unParameterizedDataType = get_type(oneOf)|un_parameterize_type %}
74-
// deserialize {{ unParameterizedDataType }}
73+
{%- set parameterizedDataType = get_type(oneOf) %}
74+
{%- set unParameterizedDataType = parameterizedDataType|un_parameterize_type %}
75+
{%- set isParameterized = parameterizedDataType|is_parameterized_type %}
76+
// deserialize {{ parameterizedDataType }}
7577
try {
7678
boolean attemptParsing = true;
7779
// ensure that we respect type coercion as set on the client ObjectMapper
@@ -88,7 +90,11 @@ public class {{ name }} extends AbstractOpenApiSchema {
8890
}
8991
}
9092
if (attemptParsing) {
93+
{%- if isParameterized %}
94+
tmp = tree.traverse(jp.getCodec()).readValueAs(new TypeReference<{{ parameterizedDataType }}>() {});
95+
{%- else %}
9196
tmp = tree.traverse(jp.getCodec()).readValueAs({{ unParameterizedDataType }}.class);
97+
{%- endif %}
9298
// TODO: there is no validation against JSON schema constraints
9399
// (min, max, enum, pattern...), this does not perform a strict JSON
94100
// validation, which means the 'match' count may be higher than it should be.
@@ -101,11 +107,11 @@ public class {{ name }} extends AbstractOpenApiSchema {
101107
deserialized = tmp;
102108
match++;
103109
{% endif %}
104-
log.log(Level.FINER, "Input data matches schema '{{ unParameterizedDataType }}'");
110+
log.log(Level.FINER, "Input data matches schema '{{ parameterizedDataType }}'");
105111
}
106112
} catch (Exception e) {
107113
// deserialization failed, continue
108-
log.log(Level.FINER, "Input data does not match schema '{{ unParameterizedDataType }}'", e);
114+
log.log(Level.FINER, "Input data does not match schema '{{ parameterizedDataType }}'", e);
109115
}
110116
{# #}
111117
{%- endfor %}

src/main/java/com/datadog/api/client/v1/model/DistributionPointItem.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public DistributionPointItem deserialize(JsonParser jp, DeserializationContext c
121121
log.log(Level.FINER, "Input data does not match schema 'Double'", e);
122122
}
123123

124-
// deserialize List
124+
// deserialize List<Double>
125125
try {
126126
boolean attemptParsing = true;
127127
// ensure that we respect type coercion as set on the client ObjectMapper
@@ -147,18 +147,18 @@ public DistributionPointItem deserialize(JsonParser jp, DeserializationContext c
147147
}
148148
}
149149
if (attemptParsing) {
150-
tmp = tree.traverse(jp.getCodec()).readValueAs(List.class);
150+
tmp = tree.traverse(jp.getCodec()).readValueAs(new TypeReference<List<Double>>() {});
151151
// TODO: there is no validation against JSON schema constraints
152152
// (min, max, enum, pattern...), this does not perform a strict JSON
153153
// validation, which means the 'match' count may be higher than it should be.
154154
deserialized = tmp;
155155
match++;
156156

157-
log.log(Level.FINER, "Input data matches schema 'List'");
157+
log.log(Level.FINER, "Input data matches schema 'List<Double>'");
158158
}
159159
} catch (Exception e) {
160160
// deserialization failed, continue
161-
log.log(Level.FINER, "Input data does not match schema 'List'", e);
161+
log.log(Level.FINER, "Input data does not match schema 'List<Double>'", e);
162162
}
163163

164164
DistributionPointItem ret = new DistributionPointItem();

src/main/java/com/datadog/api/client/v1/model/SharedDashboardInvitesData.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public SharedDashboardInvitesData deserialize(JsonParser jp, DeserializationCont
127127
Level.FINER, "Input data does not match schema 'SharedDashboardInvitesDataObject'", e);
128128
}
129129

130-
// deserialize List
130+
// deserialize List<SharedDashboardInvitesDataObject>
131131
try {
132132
boolean attemptParsing = true;
133133
// ensure that we respect type coercion as set on the client ObjectMapper
@@ -153,18 +153,24 @@ public SharedDashboardInvitesData deserialize(JsonParser jp, DeserializationCont
153153
}
154154
}
155155
if (attemptParsing) {
156-
tmp = tree.traverse(jp.getCodec()).readValueAs(List.class);
156+
tmp =
157+
tree.traverse(jp.getCodec())
158+
.readValueAs(new TypeReference<List<SharedDashboardInvitesDataObject>>() {});
157159
// TODO: there is no validation against JSON schema constraints
158160
// (min, max, enum, pattern...), this does not perform a strict JSON
159161
// validation, which means the 'match' count may be higher than it should be.
160162
deserialized = tmp;
161163
match++;
162164

163-
log.log(Level.FINER, "Input data matches schema 'List'");
165+
log.log(
166+
Level.FINER, "Input data matches schema 'List<SharedDashboardInvitesDataObject>'");
164167
}
165168
} catch (Exception e) {
166169
// deserialization failed, continue
167-
log.log(Level.FINER, "Input data does not match schema 'List'", e);
170+
log.log(
171+
Level.FINER,
172+
"Input data does not match schema 'List<SharedDashboardInvitesDataObject>'",
173+
e);
168174
}
169175

170176
SharedDashboardInvitesData ret = new SharedDashboardInvitesData();

src/main/java/com/datadog/api/client/v2/model/ActionQuerySpecInputs.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public ActionQuerySpecInputs deserialize(JsonParser jp, DeserializationContext c
120120
log.log(Level.FINER, "Input data does not match schema 'String'", e);
121121
}
122122

123-
// deserialize Map
123+
// deserialize Map<String, Object>
124124
try {
125125
boolean attemptParsing = true;
126126
// ensure that we respect type coercion as set on the client ObjectMapper
@@ -146,18 +146,19 @@ public ActionQuerySpecInputs deserialize(JsonParser jp, DeserializationContext c
146146
}
147147
}
148148
if (attemptParsing) {
149-
tmp = tree.traverse(jp.getCodec()).readValueAs(Map.class);
149+
tmp =
150+
tree.traverse(jp.getCodec()).readValueAs(new TypeReference<Map<String, Object>>() {});
150151
// TODO: there is no validation against JSON schema constraints
151152
// (min, max, enum, pattern...), this does not perform a strict JSON
152153
// validation, which means the 'match' count may be higher than it should be.
153154
deserialized = tmp;
154155
match++;
155156

156-
log.log(Level.FINER, "Input data matches schema 'Map'");
157+
log.log(Level.FINER, "Input data matches schema 'Map<String, Object>'");
157158
}
158159
} catch (Exception e) {
159160
// deserialization failed, continue
160-
log.log(Level.FINER, "Input data does not match schema 'Map'", e);
161+
log.log(Level.FINER, "Input data does not match schema 'Map<String, Object>'", e);
161162
}
162163

163164
ActionQuerySpecInputs ret = new ActionQuerySpecInputs();

src/main/java/com/datadog/api/client/v2/model/CIAppCreatePipelineEventRequestDataSingleOrArray.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public CIAppCreatePipelineEventRequestDataSingleOrArray deserialize(
139139
e);
140140
}
141141

142-
// deserialize List
142+
// deserialize List<CIAppCreatePipelineEventRequestData>
143143
try {
144144
boolean attemptParsing = true;
145145
// ensure that we respect type coercion as set on the client ObjectMapper
@@ -165,18 +165,24 @@ public CIAppCreatePipelineEventRequestDataSingleOrArray deserialize(
165165
}
166166
}
167167
if (attemptParsing) {
168-
tmp = tree.traverse(jp.getCodec()).readValueAs(List.class);
168+
tmp =
169+
tree.traverse(jp.getCodec())
170+
.readValueAs(new TypeReference<List<CIAppCreatePipelineEventRequestData>>() {});
169171
// TODO: there is no validation against JSON schema constraints
170172
// (min, max, enum, pattern...), this does not perform a strict JSON
171173
// validation, which means the 'match' count may be higher than it should be.
172174
deserialized = tmp;
173175
match++;
174176

175-
log.log(Level.FINER, "Input data matches schema 'List'");
177+
log.log(
178+
Level.FINER, "Input data matches schema 'List<CIAppCreatePipelineEventRequestData>'");
176179
}
177180
} catch (Exception e) {
178181
// deserialization failed, continue
179-
log.log(Level.FINER, "Input data does not match schema 'List'", e);
182+
log.log(
183+
Level.FINER,
184+
"Input data does not match schema 'List<CIAppCreatePipelineEventRequestData>'",
185+
e);
180186
}
181187

182188
CIAppCreatePipelineEventRequestDataSingleOrArray ret =

0 commit comments

Comments
 (0)