Repository navigation
fix(csharp): serialize object oneOf variants #25549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AndreyVMarkelov
wants to merge
2
commits into
OpenAPITools:master
Choose a base branch
from
AndreyVMarkelov:andreymarkelov/fix-csharp-oneof-serialization
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,715
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
.../java/org/openapitools/codegen/csharpnetcore/CSharpGenericHostOneOfSerializationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * Copyright 2026 OpenAPI-Generator Contributors (https://openapi-generator.tech) | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
| package org.openapitools.codegen.csharpnetcore; | ||
|
|
||
| import org.openapitools.codegen.DefaultGenerator; | ||
| import org.openapitools.codegen.config.CodegenConfigurator; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.nio.file.Path; | ||
|
|
||
| import static org.openapitools.codegen.TestUtils.assertFileContains; | ||
| import static org.openapitools.codegen.TestUtils.newTempFolder; | ||
|
|
||
| public class CSharpGenericHostOneOfSerializationTest { | ||
| @Test | ||
| public void testObjectOneOfWithoutDiscriminatorWritesVariant() throws Exception { | ||
| Path output = newTempFolder(); | ||
| CodegenConfigurator config = new CodegenConfigurator().setGeneratorName("csharp").setLibrary("generichost") | ||
| .setInputSpec("src/test/resources/3_0/csharp/oneof-object-serialization.yaml") | ||
| .setOutputDir(output.toString()).addAdditionalProperty("packageName", "OneOfRepro"); | ||
| new DefaultGenerator().opts(config.toClientOptInput()).generate(); | ||
| assertFileContains(output.resolve("src/OneOfRepro/Model/Entry.cs"), | ||
| "if (entry.FileEntry != null)", | ||
| "JsonSerializer.Serialize(variant, variant.GetType(), variantOptions)", | ||
| "if (entry.FolderEntry != null)", | ||
| "Multiple oneOf variants are set for Entry."); | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
modules/openapi-generator/src/test/resources/3_0/csharp/oneof-object-serialization.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| openapi: 3.0.3 | ||
| info: | ||
| title: oneOf reproduction | ||
| version: 1.0.0 | ||
| paths: {} | ||
| components: | ||
| schemas: | ||
| Entry: | ||
| oneOf: | ||
| - $ref: '#/components/schemas/FileEntry' | ||
| - $ref: '#/components/schemas/FolderEntry' | ||
| FileEntry: | ||
| type: object | ||
| required: [name, size] | ||
| properties: | ||
| name: | ||
| type: string | ||
| size: | ||
| type: integer | ||
| FolderEntry: | ||
| type: object | ||
| required: [name, id] | ||
| properties: | ||
| name: | ||
| type: string | ||
| id: | ||
| type: string |
106 changes: 106 additions & 0 deletions
106
...enerator/src/test/resources/3_0/csharp/oneof-runtime/OneOfSerializationRegressionTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| using System; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Org.OpenAPITools.Client; | ||
| using Org.OpenAPITools.Model; | ||
| using Xunit; | ||
|
|
||
| namespace Org.OpenAPITools.Test.Model | ||
| { | ||
| public class OneOfSerializationRegressionTests | ||
| { | ||
| [Fact] | ||
| public void WritesActiveObjectVariantAndWrapperProperties() | ||
| { | ||
| JsonSerializerOptions? options = null; | ||
| new HostConfiguration(new ServiceCollection()).ConfigureJsonOptions(value => options = value); | ||
| Assert.NotNull(options); | ||
| Fruit fruit = JsonSerializer.Deserialize<Fruit>("{\"kind\":\"gala\",\"color\":\"red\"}", options)!; | ||
| Assert.NotNull(fruit.Apple); | ||
| using (JsonDocument json = JsonDocument.Parse(JsonSerializer.Serialize(fruit, options))) | ||
| { | ||
| Assert.Equal("gala", json.RootElement.GetProperty("kind").GetString()); | ||
| Assert.Equal("red", json.RootElement.GetProperty("color").GetString()); | ||
| } | ||
|
|
||
| fruit.Apple = null; | ||
| fruit.Banana = new Banana { Count = 2 }; | ||
| using (JsonDocument json = JsonDocument.Parse(JsonSerializer.Serialize(fruit, options))) | ||
| { | ||
| Assert.Equal(2, json.RootElement.GetProperty("count").GetInt32()); | ||
| Assert.Equal("red", json.RootElement.GetProperty("color").GetString()); | ||
| } | ||
| } | ||
|
|
||
| private static Fruit CreateFruit() | ||
| { | ||
| JsonSerializerOptions? options = null; | ||
| new HostConfiguration(new ServiceCollection()).ConfigureJsonOptions(value => options = value); | ||
| return JsonSerializer.Deserialize<Fruit>("{\"kind\":\"gala\"}", options!)!; | ||
| } | ||
|
|
||
| [Fact] | ||
| public void RejectsMultipleVariants() | ||
| { | ||
| Fruit fruit = CreateFruit(); | ||
| fruit.Banana = new Banana { Count = 2 }; | ||
| var options = new JsonSerializerOptions(); | ||
| options.Converters.Add(new FruitJsonConverter()); | ||
| Assert.Throws<JsonException>(() => JsonSerializer.Serialize(fruit, options)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void UsesGeneratedVariantConverterWhenNotRegistered() | ||
| { | ||
| Fruit fruit = CreateFruit(); | ||
| var options = new JsonSerializerOptions(); | ||
| options.Converters.Add(new FruitJsonConverter()); | ||
| using JsonDocument json = JsonDocument.Parse(JsonSerializer.Serialize(fruit, options)); | ||
| Assert.Equal("gala", json.RootElement.GetProperty("kind").GetString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void UsesCustomVariantConverter() | ||
| { | ||
| Fruit fruit = CreateFruit(); | ||
| var options = new JsonSerializerOptions(); | ||
| options.Converters.Add(new FruitJsonConverter()); | ||
| options.Converters.Add(new CustomAppleConverter()); | ||
| using JsonDocument json = JsonDocument.Parse(JsonSerializer.Serialize(fruit, options)); | ||
| Assert.Equal("custom", json.RootElement.GetProperty("kind").GetString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void UsesDerivedVariantConverter() | ||
| { | ||
| Fruit fruit = CreateFruit(); | ||
| fruit.Apple = new FancyApple(); | ||
| var options = new JsonSerializerOptions(); | ||
| options.Converters.Add(new FruitJsonConverter()); | ||
| options.Converters.Add(new FancyAppleConverter()); | ||
| using JsonDocument json = JsonDocument.Parse(JsonSerializer.Serialize(fruit, options)); | ||
| Assert.Equal("fancy", json.RootElement.GetProperty("extra").GetString()); | ||
| } | ||
|
|
||
| private sealed class CustomAppleConverter : JsonConverter<Apple> | ||
| { | ||
| public override Apple Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options) => throw new NotSupportedException(); | ||
| public override void Write(Utf8JsonWriter writer, Apple value, JsonSerializerOptions options) | ||
| { | ||
| writer.WriteStartObject(); writer.WriteString("kind", "custom"); writer.WriteEndObject(); | ||
| } | ||
| } | ||
|
|
||
| private sealed class FancyApple : Apple { } | ||
|
|
||
| private sealed class FancyAppleConverter : JsonConverter<FancyApple> | ||
| { | ||
| public override FancyApple Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options) => throw new NotSupportedException(); | ||
| public override void Write(Utf8JsonWriter writer, FancyApple value, JsonSerializerOptions options) | ||
| { | ||
| writer.WriteStartObject(); writer.WriteString("extra", "fancy"); writer.WriteEndObject(); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.