Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,25 @@
{{#lambda.trimLineBreaks}}
int currentDepth = utf8JsonReader.CurrentDepth;

{{#composedSchemas.oneOf}}
{{#-first}}
bool oneOfHasPrimitiveType = false;
{{/-first}}
{{#isPrimitiveType}}
oneOfHasPrimitiveType = true;
{{/isPrimitiveType}}
{{#-last}}

if (!oneOfHasPrimitiveType &&
utf8JsonReader.TokenType != JsonTokenType.StartObject &&
utf8JsonReader.TokenType != JsonTokenType.StartArray)
throw new JsonException();
{{/-last}}
{{/composedSchemas.oneOf}}
{{^composedSchemas.oneOf}}
if (utf8JsonReader.TokenType != JsonTokenType.StartObject && utf8JsonReader.TokenType != JsonTokenType.StartArray)
throw new JsonException();
{{/composedSchemas.oneOf}}

JsonTokenType startingTokenType = utf8JsonReader.TokenType;

Expand Down Expand Up @@ -113,24 +130,40 @@
{{{datatypeWithEnum}}}{{nrt?}}{{^nrt}}{{#vendorExtensions.x-is-value-type}}?{{/vendorExtensions.x-is-value-type}}{{/nrt}} {{#lambda.camelcase_sanitize_param}}{{name}}{{/lambda.camelcase_sanitize_param}} = default;
{{#-last}}

Utf8JsonReader utf8JsonReaderOneOf = utf8JsonReader;
while (utf8JsonReaderOneOf.Read())
{
if (startingTokenType == JsonTokenType.StartObject && utf8JsonReaderOneOf.TokenType == JsonTokenType.EndObject && currentDepth == utf8JsonReaderOneOf.CurrentDepth)
break;

if (startingTokenType == JsonTokenType.StartArray && utf8JsonReaderOneOf.TokenType == JsonTokenType.EndArray && currentDepth == utf8JsonReaderOneOf.CurrentDepth)
break;
if (oneOfHasPrimitiveType &&
startingTokenType != JsonTokenType.StartObject &&
startingTokenType != JsonTokenType.StartArray)
{
{{#oneOf}}
Utf8JsonReader utf8JsonReader{{name}} = utf8JsonReader;
ClientUtils.TryDeserialize<{{{datatypeWithEnum}}}{{nrt?}}{{^nrt}}{{#vendorExtensions.x-is-value-type}}?{{/vendorExtensions.x-is-value-type}}{{/nrt}}>(ref utf8JsonReader{{name}}, jsonSerializerOptions, out {{#lambda.camelcase_sanitize_param}}{{name}}{{/lambda.camelcase_sanitize_param}});
{{^-last}}

if (utf8JsonReaderOneOf.TokenType == JsonTokenType.PropertyName && currentDepth == utf8JsonReaderOneOf.CurrentDepth - 1)
{{/-last}}
{{/oneOf}}
}
else
{
Utf8JsonReader utf8JsonReaderOneOf = utf8JsonReader;
while (utf8JsonReaderOneOf.Read())
{
{{#oneOf}}
Utf8JsonReader utf8JsonReader{{name}} = utf8JsonReader;
ClientUtils.TryDeserialize<{{{datatypeWithEnum}}}{{nrt?}}{{^nrt}}{{#vendorExtensions.x-is-value-type}}?{{/vendorExtensions.x-is-value-type}}{{/nrt}}>(ref utf8JsonReader{{name}}, jsonSerializerOptions, out {{#lambda.camelcase_sanitize_param}}{{name}}{{/lambda.camelcase_sanitize_param}});
{{^-last}}
if (startingTokenType == JsonTokenType.StartObject && utf8JsonReaderOneOf.TokenType == JsonTokenType.EndObject && currentDepth == utf8JsonReaderOneOf.CurrentDepth)
break;

{{/-last}}
{{/oneOf}}
if (startingTokenType == JsonTokenType.StartArray && utf8JsonReaderOneOf.TokenType == JsonTokenType.EndArray && currentDepth == utf8JsonReaderOneOf.CurrentDepth)
break;

if (utf8JsonReaderOneOf.TokenType == JsonTokenType.PropertyName && currentDepth == utf8JsonReaderOneOf.CurrentDepth - 1)
{
{{#oneOf}}
Utf8JsonReader utf8JsonReader{{name}} = utf8JsonReader;
ClientUtils.TryDeserialize<{{{datatypeWithEnum}}}{{nrt?}}{{^nrt}}{{#vendorExtensions.x-is-value-type}}?{{/vendorExtensions.x-is-value-type}}{{/nrt}}>(ref utf8JsonReader{{name}}, jsonSerializerOptions, out {{#lambda.camelcase_sanitize_param}}{{name}}{{/lambda.camelcase_sanitize_param}});
{{^-last}}

{{/-last}}
{{/oneOf}}
}
}
}
{{/-last}}
Expand Down Expand Up @@ -376,7 +409,51 @@

{{/children}}
{{/discriminator}}
{{^model.discriminator}}
{{#composedSchemas}}
{{#oneOf}}
{{#isPrimitiveType}}
{{#isContainer}}
if ({{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}} != null)
{
JsonSerializer.Serialize(writer, {{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}}, jsonSerializerOptions);
return;
}

{{/isContainer}}
{{^isContainer}}
{{^vendorExtensions.x-duplicated-data-type}}
if ({{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}} != null)
{
JsonSerializer.Serialize(writer, {{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}}, jsonSerializerOptions);
return;
}

{{/vendorExtensions.x-duplicated-data-type}}
{{/isContainer}}
{{/isPrimitiveType}}
{{/oneOf}}
{{/composedSchemas}}
{{/model.discriminator}}
writer.WriteStartObject();
{{^model.discriminator}}
{{#composedSchemas}}
{{#oneOf}}
{{^vendorExtensions.x-duplicated-data-type}}
{{#isModel}}
{{^isContainer}}
if ({{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}} != null)
{
{{baseType}}JsonConverter {{#lambda.camelcase_sanitize_param}}{{baseType}}JsonConverter{{/lambda.camelcase_sanitize_param}} = ({{baseType}}JsonConverter) jsonSerializerOptions.Converters.First(c => c.CanConvert({{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}}.GetType()));
{{#lambda.camelcase_sanitize_param}}{{baseType}}JsonConverter{{/lambda.camelcase_sanitize_param}}.WriteProperties(writer, {{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}}, jsonSerializerOptions);
}

{{/isContainer}}
{{/isModel}}
{{/vendorExtensions.x-duplicated-data-type}}
{{/oneOf}}
{{/composedSchemas}}
{{/model.discriminator}}

{{#model.discriminator}}
{{#model.hasDiscriminatorWithNonEmptyMapping}}
Expand Down Expand Up @@ -678,4 +755,4 @@
{{/lambda.trimLineBreaks}}
{{/lambda.trimTrailingWithNewLine}}
}
}
}
106 changes: 64 additions & 42 deletions modules/openapi-generator/src/test/resources/3_0/oneOf.yaml
Original file line number Diff line number Diff line change
@@ -1,46 +1,68 @@
openapi: 3.0.1

info:
title: fruity
version: 0.0.1
title: fruity
version: 0.0.1

paths:
/:
get:
responses:
'200':
description: desc
content:
application/json:
schema:
$ref: '#/components/schemas/fruit'
/:
get:
responses:
'200':
description: desc
content:
application/json:
schema:
$ref: '#/components/schemas/fruit'

components:
schemas:
fruit:
title: fruit
properties:
color:
type: string
oneOf:
- $ref: '#/components/schemas/apple'
- $ref: '#/components/schemas/banana'
- $ref: '#/components/schemas/orange'
# additionalProperties:
# type: string
# uncomment this when https://github.com/swagger-api/swagger-parser/issues/1252 is resolved
apple:
title: apple
type: object
properties:
kind:
type: string
banana:
title: banana
type: object
properties:
count:
type: number
orange:
title: orange
type: object
properties:
sweet:
type: boolean
schemas:
fruit:
title: fruit
properties:
color:
type: string
oneOf:
- $ref: '#/components/schemas/apple'
- $ref: '#/components/schemas/banana'
- $ref: '#/components/schemas/orange'

inlineOneOfModel:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: The two newly added schemas inlineOneOfModel and union are not referenced by any test. A repository-wide search shows the only consumers of this shared fixture (DefaultCodegenTest, RubyClientCodegenTest, JavaClientCodegenTest, ProtobufSchemaCodegenTest, Swift6ClientCodegenTest) all target only the pre-existing fruit schema, and no test or sample references inlineOneOfModel or union. They therefore add no regression coverage (the PR's referenced-model tests use fruit) and leave dead content in a shared test resource. Add a test that exercises these schemas, or drop them.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/test/resources/3_0/oneOf.yaml, line 30:

<comment>The two newly added schemas `inlineOneOfModel` and `union` are not referenced by any test. A repository-wide search shows the only consumers of this shared fixture (`DefaultCodegenTest`, `RubyClientCodegenTest`, `JavaClientCodegenTest`, `ProtobufSchemaCodegenTest`, `Swift6ClientCodegenTest`) all target only the pre-existing `fruit` schema, and no test or sample references `inlineOneOfModel` or `union`. They therefore add no regression coverage (the PR's referenced-model tests use `fruit`) and leave dead content in a shared test resource. Add a test that exercises these schemas, or drop them.</comment>

<file context>
@@ -1,46 +1,68 @@
+        - $ref: '#/components/schemas/banana'
+        - $ref: '#/components/schemas/orange'
+
+    inlineOneOfModel:
+      title: inlineOneOfModel
+      type: object
</file context>

title: inlineOneOfModel
type: object
properties:
value:
oneOf:
- type: string
- type: integer

union:
title: union
oneOf:
- type: string
- type: integer

apple:
title: apple
type: object
properties:
kind:
type: string

banana:
title: banana
type: object
properties:
count:
type: number

orange:
title: orange
type: object
properties:
sweet:
type: boolean

# additionalProperties:
# type: string
# uncomment this when https://github.com/swagger-api/swagger-parser/issues/1252 is resolved
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ public override OneOfNullableTest Read(ref Utf8JsonReader utf8JsonReader, Type t
/// <exception cref="NotImplementedException"></exception>
public override void Write(Utf8JsonWriter writer, OneOfNullableTest oneOfNullableTest, JsonSerializerOptions jsonSerializerOptions)
{
if (oneOfNullableTest.Int != null)
{
JsonSerializer.Serialize(writer, oneOfNullableTest.Int, jsonSerializerOptions);
return;
}

if (oneOfNullableTest.String != null)
{
JsonSerializer.Serialize(writer, oneOfNullableTest.String, jsonSerializerOptions);
return;
}

writer.WriteStartObject();

WriteProperties(writer, oneOfNullableTest, jsonSerializerOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ public override ParentWithPluralOneOfPropertyNumber Read(ref Utf8JsonReader utf8
/// <exception cref="NotImplementedException"></exception>
public override void Write(Utf8JsonWriter writer, ParentWithPluralOneOfPropertyNumber parentWithPluralOneOfPropertyNumber, JsonSerializerOptions jsonSerializerOptions)
{
if (parentWithPluralOneOfPropertyNumber.Number != null)
{
JsonSerializer.Serialize(writer, parentWithPluralOneOfPropertyNumber.Number, jsonSerializerOptions);
return;
}

if (parentWithPluralOneOfPropertyNumber.Number2 != null)
{
JsonSerializer.Serialize(writer, parentWithPluralOneOfPropertyNumber.Number2, jsonSerializerOptions);
return;
}

writer.WriteStartObject();

WriteProperties(writer, parentWithPluralOneOfPropertyNumber, jsonSerializerOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Linq;
using System.IO;
using System.Collections.Generic;
using System.Text.Json;
using Org.OpenAPITools.Model;
using Org.OpenAPITools.Client;
using System.Reflection;
Expand Down Expand Up @@ -52,5 +53,31 @@ public void OneOfArrayRequestInstanceTest()
// TODO uncomment below to test "IsType" OneOfArrayRequest
//Assert.IsType<OneOfArrayRequest>(instance);
}

[Fact]
public void SerializeOneOfStringArray()
{
var options = new JsonSerializerOptions();
options.Converters.Add(new OneOfArrayRequestJsonConverter());
var request = new OneOfArrayRequest(new List<string> { "first", "second" });

string json = JsonSerializer.Serialize(request, options);

Assert.Equal("[\"first\",\"second\"]", json);
}

[Fact]
public void SerializeOneOfModelArray()
{
var options = new JsonSerializerOptions();
options.Converters.Add(new OneOfArrayRequestJsonConverter());
options.Converters.Add(new TestObjectJsonConverter());
var item = new TestObject(new Option<string?>("example"));
var request = new OneOfArrayRequest(new List<TestObject> { item });

string json = JsonSerializer.Serialize(request, options);

Assert.Equal("[{\"name\":\"example\"}]", json);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ public override OneOfArrayRequest Read(ref Utf8JsonReader utf8JsonReader, Type t
/// <exception cref="NotImplementedException"></exception>
public override void Write(Utf8JsonWriter writer, OneOfArrayRequest oneOfArrayRequest, JsonSerializerOptions jsonSerializerOptions)
{
if (oneOfArrayRequest.List != null)
{
JsonSerializer.Serialize(writer, oneOfArrayRequest.List, jsonSerializerOptions);
return;
}

if (oneOfArrayRequest.List1 != null)
{
JsonSerializer.Serialize(writer, oneOfArrayRequest.List1, jsonSerializerOptions);
return;
}

writer.WriteStartObject();

WriteProperties(writer, oneOfArrayRequest, jsonSerializerOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,17 @@ public override Fruit Read(ref Utf8JsonReader utf8JsonReader, Type typeToConvert
public override void Write(Utf8JsonWriter writer, Fruit fruit, JsonSerializerOptions jsonSerializerOptions)
{
writer.WriteStartObject();
if (fruit.Apple != null)
{
AppleJsonConverter appleJsonConverter = (AppleJsonConverter) jsonSerializerOptions.Converters.First(c => c.CanConvert(fruit.Apple.GetType()));
appleJsonConverter.WriteProperties(writer, fruit.Apple, jsonSerializerOptions);
}

if (fruit.Banana != null)
{
BananaJsonConverter bananaJsonConverter = (BananaJsonConverter) jsonSerializerOptions.Converters.First(c => c.CanConvert(fruit.Banana.GetType()));
bananaJsonConverter.WriteProperties(writer, fruit.Banana, jsonSerializerOptions);
}

WriteProperties(writer, fruit, jsonSerializerOptions);
writer.WriteEndObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ public override FruitReq Read(ref Utf8JsonReader utf8JsonReader, Type typeToConv
public override void Write(Utf8JsonWriter writer, FruitReq fruitReq, JsonSerializerOptions jsonSerializerOptions)
{
writer.WriteStartObject();
if (fruitReq.AppleReq != null)
{
AppleReqJsonConverter appleReqJsonConverter = (AppleReqJsonConverter) jsonSerializerOptions.Converters.First(c => c.CanConvert(fruitReq.AppleReq.GetType()));
appleReqJsonConverter.WriteProperties(writer, fruitReq.AppleReq, jsonSerializerOptions);
}

if (fruitReq.BananaReq != null)
{
BananaReqJsonConverter bananaReqJsonConverter = (BananaReqJsonConverter) jsonSerializerOptions.Converters.First(c => c.CanConvert(fruitReq.BananaReq.GetType()));
bananaReqJsonConverter.WriteProperties(writer, fruitReq.BananaReq, jsonSerializerOptions);
}

WriteProperties(writer, fruitReq, jsonSerializerOptions);
writer.WriteEndObject();
Expand Down
Loading
Loading