-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[http-client-csharp] fix: generate deserialization switch for nested …
- Loading branch information
1 parent
d1bcbf2
commit aa531cf
Showing
3 changed files
with
182 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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
127 changes: 127 additions & 0 deletions
127
...finitions/TestData/DiscriminatorTests/TestNestedDiscriminatedModelWithOwnDiscriminator.cs
This file contains 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,127 @@ | ||
// <auto-generated/> | ||
|
||
#nullable disable | ||
|
||
using System; | ||
using System.ClientModel; | ||
using System.ClientModel.Primitives; | ||
using System.Text.Json; | ||
using Sample; | ||
|
||
namespace Sample.Models | ||
{ | ||
/// <summary></summary> | ||
public partial class Tree : global::System.ClientModel.Primitives.IJsonModel<global::Sample.Models.Tree> | ||
{ | ||
internal Tree() | ||
{ | ||
} | ||
|
||
void global::System.ClientModel.Primitives.IJsonModel<global::Sample.Models.Tree>.Write(global::System.Text.Json.Utf8JsonWriter writer, global::System.ClientModel.Primitives.ModelReaderWriterOptions options) | ||
{ | ||
writer.WriteStartObject(); | ||
this.JsonModelWriteCore(writer, options); | ||
writer.WriteEndObject(); | ||
} | ||
|
||
/// <param name="writer"> The JSON writer. </param> | ||
/// <param name="options"> The client options for reading and writing models. </param> | ||
protected override void JsonModelWriteCore(global::System.Text.Json.Utf8JsonWriter writer, global::System.ClientModel.Primitives.ModelReaderWriterOptions options) | ||
{ | ||
string format = (options.Format == "W") ? ((global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.Tree>)this).GetFormatFromOptions(options) : options.Format; | ||
if ((format != "J")) | ||
{ | ||
throw new global::System.FormatException($"The model {nameof(global::Sample.Models.Tree)} does not support writing '{format}' format."); | ||
} | ||
base.JsonModelWriteCore(writer, options); | ||
writer.WritePropertyName("treeType"u8); | ||
writer.WriteStringValue(TreeType); | ||
} | ||
|
||
global::Sample.Models.Tree global::System.ClientModel.Primitives.IJsonModel<global::Sample.Models.Tree>.Create(ref global::System.Text.Json.Utf8JsonReader reader, global::System.ClientModel.Primitives.ModelReaderWriterOptions options) => ((global::Sample.Models.Tree)this.JsonModelCreateCore(ref reader, options)); | ||
|
||
/// <param name="reader"> The JSON reader. </param> | ||
/// <param name="options"> The client options for reading and writing models. </param> | ||
protected override global::Sample.Models.Plant JsonModelCreateCore(ref global::System.Text.Json.Utf8JsonReader reader, global::System.ClientModel.Primitives.ModelReaderWriterOptions options) | ||
{ | ||
string format = (options.Format == "W") ? ((global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.Tree>)this).GetFormatFromOptions(options) : options.Format; | ||
if ((format != "J")) | ||
{ | ||
throw new global::System.FormatException($"The model {nameof(global::Sample.Models.Tree)} does not support reading '{format}' format."); | ||
} | ||
using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.ParseValue(ref reader); | ||
return global::Sample.Models.Tree.DeserializeTree(document.RootElement, options); | ||
} | ||
|
||
internal static global::Sample.Models.Tree DeserializeTree(global::System.Text.Json.JsonElement element, global::System.ClientModel.Primitives.ModelReaderWriterOptions options) | ||
{ | ||
if ((element.ValueKind == global::System.Text.Json.JsonValueKind.Null)) | ||
{ | ||
return null; | ||
} | ||
if (element.TryGetProperty("treeType"u8, out global::System.Text.Json.JsonElement discriminator)) | ||
{ | ||
switch (discriminator.GetString()) | ||
{ | ||
case "oak": | ||
return global::Sample.Models.OakTree.DeserializeOakTree(element, options); | ||
} | ||
} | ||
return global::Sample.Models.UnknownTree.DeserializeUnknownTree(element, options); | ||
} | ||
|
||
global::System.BinaryData global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.Tree>.Write(global::System.ClientModel.Primitives.ModelReaderWriterOptions options) => this.PersistableModelWriteCore(options); | ||
|
||
/// <param name="options"> The client options for reading and writing models. </param> | ||
protected override global::System.BinaryData PersistableModelWriteCore(global::System.ClientModel.Primitives.ModelReaderWriterOptions options) | ||
{ | ||
string format = (options.Format == "W") ? ((global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.Tree>)this).GetFormatFromOptions(options) : options.Format; | ||
switch (format) | ||
{ | ||
case "J": | ||
return global::System.ClientModel.Primitives.ModelReaderWriter.Write(this, options); | ||
default: | ||
throw new global::System.FormatException($"The model {nameof(global::Sample.Models.Tree)} does not support writing '{options.Format}' format."); | ||
} | ||
} | ||
|
||
global::Sample.Models.Tree global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.Tree>.Create(global::System.BinaryData data, global::System.ClientModel.Primitives.ModelReaderWriterOptions options) => ((global::Sample.Models.Tree)this.PersistableModelCreateCore(data, options)); | ||
|
||
/// <param name="data"> The data to parse. </param> | ||
/// <param name="options"> The client options for reading and writing models. </param> | ||
protected override global::Sample.Models.Plant PersistableModelCreateCore(global::System.BinaryData data, global::System.ClientModel.Primitives.ModelReaderWriterOptions options) | ||
{ | ||
string format = (options.Format == "W") ? ((global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.Tree>)this).GetFormatFromOptions(options) : options.Format; | ||
switch (format) | ||
{ | ||
case "J": | ||
using (global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(data)) | ||
{ | ||
return global::Sample.Models.Tree.DeserializeTree(document.RootElement, options); | ||
} | ||
default: | ||
throw new global::System.FormatException($"The model {nameof(global::Sample.Models.Tree)} does not support reading '{options.Format}' format."); | ||
} | ||
} | ||
|
||
string global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.Tree>.GetFormatFromOptions(global::System.ClientModel.Primitives.ModelReaderWriterOptions options) => "J"; | ||
|
||
/// <param name="tree"> The <see cref="global::Sample.Models.Tree"/> to serialize into <see cref="global::System.ClientModel.BinaryContent"/>. </param> | ||
public static implicit operator BinaryContent(global::Sample.Models.Tree tree) | ||
{ | ||
if ((tree == null)) | ||
{ | ||
return null; | ||
} | ||
return global::System.ClientModel.BinaryContent.Create(tree, global::Sample.ModelSerializationExtensions.WireOptions); | ||
} | ||
|
||
/// <param name="result"> The <see cref="global::System.ClientModel.ClientResult"/> to deserialize the <see cref="global::Sample.Models.Tree"/> from. </param> | ||
public static explicit operator Tree(global::System.ClientModel.ClientResult result) | ||
{ | ||
using global::System.ClientModel.Primitives.PipelineResponse response = result.GetRawResponse(); | ||
using global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(response.Content); | ||
return global::Sample.Models.Tree.DeserializeTree(document.RootElement, global::Sample.ModelSerializationExtensions.WireOptions); | ||
} | ||
} | ||
} |