Skip to content

Commit bc16ea6

Browse files
committed
Support nested oneOf
1 parent 13770f5 commit bc16ea6

4 files changed

Lines changed: 65 additions & 6 deletions

File tree

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ public class {{ name }} extends AbstractOpenApiSchema {
8383
{%- set parameterizedDataType = get_type(oneOf) %}
8484
{%- set unParameterizedDataType = parameterizedDataType|un_parameterize_type %}
8585
{%- set isParameterized = parameterizedDataType|is_parameterized_type %}
86+
{%- set isModelMember = not oneOf|is_primitive and not unParameterizedDataType|lower|is_java_base_type and "enum" not in oneOf %}
87+
{%- set isComposedMember = "oneOf" in oneOf or "anyOf" in oneOf %}
88+
{%- set isModelListMember = "items" in oneOf and oneOf.get("items")|is_model and not oneOf|is_primitive %}
8689
// deserialize {{ parameterizedDataType }}
8790
try {
8891
boolean attemptParsing = true;
@@ -108,12 +111,20 @@ public class {{ name }} extends AbstractOpenApiSchema {
108111
// TODO: there is no validation against JSON schema constraints
109112
// (min, max, enum, pattern...), this does not perform a strict JSON
110113
// validation, which means the 'match' count may be higher than it should be.
111-
{%- if not oneOf|is_primitive and not unParameterizedDataType|lower|is_java_base_type and "enum" not in oneOf %}
112-
if (!(({{ unParameterizedDataType }})tmp).unparsed) {
114+
{%- if isModelMember %}
115+
if (!(({{ unParameterizedDataType }}) tmp).unparsed
116+
{%- if isComposedMember %}
117+
{#- unmatched oneOf doesn't propagate to "tmp.unparsed": we aim
118+
for today's client to be forward-compatible with future oneOf
119+
branches. But a oneOf branch that it itself a oneOf? We _must_
120+
respect the nested oneOf's `unparsed` or the oneOf branch will
121+
always match. #}
122+
&& !((({{ unParameterizedDataType }}) tmp).getActualInstance() instanceof UnparsedObject)
123+
{%- endif %}) {
113124
deserialized = tmp;
114125
match++;
115126
}
116-
{%- elif "items" in oneOf and oneOf.get("items")|is_model and not oneOf|is_primitive %}
127+
{%- elif isModelListMember %}
117128
{%- set itemsDataType = get_type(oneOf.get("items")) %}
118129
// keep the matched list, but propagate 'unparsed' from any invalid item
119130
boolean itemsUnparsed = false;

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ public CIAppCreatePipelineEventRequestAttributesResource deserialize(
124124
// TODO: there is no validation against JSON schema constraints
125125
// (min, max, enum, pattern...), this does not perform a strict JSON
126126
// validation, which means the 'match' count may be higher than it should be.
127-
if (!((CIAppPipelineEventPipeline) tmp).unparsed) {
127+
if (!((CIAppPipelineEventPipeline) tmp).unparsed
128+
&& !(((CIAppPipelineEventPipeline) tmp).getActualInstance()
129+
instanceof UnparsedObject)) {
128130
deserialized = tmp;
129131
match++;
130132
}
@@ -214,7 +216,8 @@ public CIAppCreatePipelineEventRequestAttributesResource deserialize(
214216
// TODO: there is no validation against JSON schema constraints
215217
// (min, max, enum, pattern...), this does not perform a strict JSON
216218
// validation, which means the 'match' count may be higher than it should be.
217-
if (!((CIAppPipelineEventJob) tmp).unparsed) {
219+
if (!((CIAppPipelineEventJob) tmp).unparsed
220+
&& !(((CIAppPipelineEventJob) tmp).getActualInstance() instanceof UnparsedObject)) {
218221
deserialized = tmp;
219222
match++;
220223
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ public UpsertCatalogEntityRequest deserialize(JsonParser jp, DeserializationCont
111111
// TODO: there is no validation against JSON schema constraints
112112
// (min, max, enum, pattern...), this does not perform a strict JSON
113113
// validation, which means the 'match' count may be higher than it should be.
114-
if (!((EntityV3) tmp).unparsed) {
114+
if (!((EntityV3) tmp).unparsed
115+
&& !(((EntityV3) tmp).getActualInstance() instanceof UnparsedObject)) {
115116
deserialized = tmp;
116117
match++;
117118
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
3+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
* Copyright 2019-Present Datadog, Inc.
5+
*/
6+
7+
package com.datadog.api.client.v2.api;
8+
9+
import static com.datadog.api.World.fromJSON;
10+
import static org.junit.Assert.assertEquals;
11+
12+
import com.datadog.api.client.v2.model.CIAppCreatePipelineEventRequestAttributesResource;
13+
import com.datadog.api.client.v2.model.CIAppPipelineEventFinishedJob;
14+
import com.fasterxml.jackson.core.JsonProcessingException;
15+
import com.fasterxml.jackson.databind.ObjectMapper;
16+
import org.junit.BeforeClass;
17+
import org.junit.Test;
18+
19+
public class CIAppPipelineEventJobTest extends V2APITest {
20+
21+
private static ObjectMapper objectMapper;
22+
23+
@Override
24+
public String getTracingEndpoint() {
25+
return "ci-app";
26+
}
27+
28+
@BeforeClass
29+
public static void initApi() {
30+
objectMapper = generalApiUnitTestClient.getJSON().getMapper();
31+
}
32+
33+
@Test
34+
public void testDeserializeNestedOneOfJob() throws JsonProcessingException {
35+
String body =
36+
"{\"end\":\"2023-01-01T12:00:01+00:00\",\"id\":\"job-id\",\"level\":\"job\",\"name\":\"job-name\",\"pipeline_name\":\"my-pipeline\",\"pipeline_unique_id\":\"pipeline-unique-id\",\"start\":\"2023-01-01T12:00:00+00:00\",\"status\":\"success\",\"url\":\"https://example.com/job\"}";
37+
CIAppCreatePipelineEventRequestAttributesResource res =
38+
fromJSON(objectMapper, CIAppCreatePipelineEventRequestAttributesResource.class, body);
39+
40+
CIAppPipelineEventFinishedJob finishedJob =
41+
res.getCIAppPipelineEventJob().getCIAppPipelineEventFinishedJob();
42+
assertEquals("job-id", finishedJob.getId());
43+
}
44+
}

0 commit comments

Comments
 (0)