From 42f25139be5f496c3e437d187bcf0fb9cbdcb58d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20C=C3=B4t=C3=A9?= Date: Tue, 15 Aug 2023 14:00:54 -0400 Subject: [PATCH 1/2] Fix problem with json serializer option --- .../csharp/presets/JsonSerializerPreset.ts | 4 ++-- .../JsonSerializerPreset.spec.ts.snap | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/generators/csharp/presets/JsonSerializerPreset.ts b/src/generators/csharp/presets/JsonSerializerPreset.ts index 578f9b3503..9179fc28e5 100644 --- a/src/generators/csharp/presets/JsonSerializerPreset.ts +++ b/src/generators/csharp/presets/JsonSerializerPreset.ts @@ -20,7 +20,7 @@ function renderSerializeProperty( model.property instanceof ConstrainedReferenceModel && model.property.ref instanceof ConstrainedEnumModel ) { - value = `${model.property.type}.GetValue()`; + value = `value.${model.property.type}.GetValue()`; } return `JsonSerializer.Serialize(writer, ${value}, options);`; } @@ -123,7 +123,7 @@ function renderDeserializeProperty(model: ConstrainedObjectPropertyModel) { model.property instanceof ConstrainedReferenceModel && model.property.ref instanceof ConstrainedEnumModel ) { - return `${model.property.name}Extension.To${model.property.name}(JsonSerializer.Deserialize(ref reader, options))`; + return `${model.property.name}Extensions.To${model.property.name}(JsonSerializer.Deserialize(ref reader, options))`; } return `JsonSerializer.Deserialize<${model.property.type}>(ref reader, options)`; } diff --git a/test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap b/test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap index 793fc7fed8..ff361b1bf2 100644 --- a/test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap +++ b/test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap @@ -114,22 +114,22 @@ internal class TestConverter : JsonConverter writer.WriteStartObject(); - if(value.StringProp != null) { + if(value.StringProp != null) { // write property name and let the serializer serialize the value itself writer.WritePropertyName(\\"string prop\\"); JsonSerializer.Serialize(writer, value.StringProp, options); } - if(value.NumberProp != null) { + if(value.NumberProp != null) { // write property name and let the serializer serialize the value itself writer.WritePropertyName(\\"numberProp\\"); JsonSerializer.Serialize(writer, value.NumberProp, options); } - if(value.EnumProp != null) { + if(value.EnumProp != null) { // write property name and let the serializer serialize the value itself writer.WritePropertyName(\\"enumProp\\"); - JsonSerializer.Serialize(writer, EnumTest?.GetValue(), options); + JsonSerializer.Serialize(writer, value.EnumTest?.GetValue(), options); } - if(value.ObjectProp != null) { + if(value.ObjectProp != null) { // write property name and let the serializer serialize the value itself writer.WritePropertyName(\\"objectProp\\"); JsonSerializer.Serialize(writer, value.ObjectProp, options); @@ -147,7 +147,7 @@ internal class TestConverter : JsonConverter writer.WritePropertyName(unwrappedProperty.Key); JsonSerializer.Serialize(writer, unwrappedProperty.Value, options); } - }if(value.AdditionalProperties != null) { + }if(value.AdditionalProperties != null) { // write property name and let the serializer serialize the value itself writer.WritePropertyName(\\"additionalProperties\\"); JsonSerializer.Serialize(writer, value.AdditionalProperties, options); @@ -274,7 +274,7 @@ internal class NestedTestConverter : JsonConverter writer.WriteStartObject(); - if(value.StringProp != null) { + if(value.StringProp != null) { // write property name and let the serializer serialize the value itself writer.WritePropertyName(\\"stringProp\\"); JsonSerializer.Serialize(writer, value.StringProp, options); @@ -292,7 +292,7 @@ internal class NestedTestConverter : JsonConverter writer.WritePropertyName(unwrappedProperty.Key); JsonSerializer.Serialize(writer, unwrappedProperty.Value, options); } - }if(value.AdditionalProperties != null) { + }if(value.AdditionalProperties != null) { // write property name and let the serializer serialize the value itself writer.WritePropertyName(\\"additionalProperties\\"); JsonSerializer.Serialize(writer, value.AdditionalProperties, options); From f20e12bc60a31981a4b59f789bf5583f10ceddc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20C=C3=B4t=C3=A9?= Date: Tue, 15 Aug 2023 14:19:14 -0400 Subject: [PATCH 2/2] Update to fix validations --- .../__snapshots__/index.spec.ts.snap | 2 +- src/generators/csharp/presets/JsonSerializerPreset.ts | 2 +- .../presets/__snapshots__/JsonSerializerPreset.spec.ts.snap | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/csharp-generate-json-serializer/__snapshots__/index.spec.ts.snap b/examples/csharp-generate-json-serializer/__snapshots__/index.spec.ts.snap index 3ae391fac8..ba6cd1f148 100644 --- a/examples/csharp-generate-json-serializer/__snapshots__/index.spec.ts.snap +++ b/examples/csharp-generate-json-serializer/__snapshots__/index.spec.ts.snap @@ -65,7 +65,7 @@ internal class RootConverter : JsonConverter writer.WriteStartObject(); - if(value.Email != null) { + if(value.Email != null) { // write property name and let the serializer serialize the value itself writer.WritePropertyName(\\"email\\"); JsonSerializer.Serialize(writer, value.Email, options); diff --git a/src/generators/csharp/presets/JsonSerializerPreset.ts b/src/generators/csharp/presets/JsonSerializerPreset.ts index 9179fc28e5..82e6252c49 100644 --- a/src/generators/csharp/presets/JsonSerializerPreset.ts +++ b/src/generators/csharp/presets/JsonSerializerPreset.ts @@ -51,7 +51,7 @@ if (${modelInstanceVariable} != null) { } }`; } - serializeProperties += `if(${modelInstanceVariable} != null) { + serializeProperties += `if(${modelInstanceVariable} != null) { // write property name and let the serializer serialize the value itself writer.WritePropertyName("${propertyModel.unconstrainedPropertyName}"); ${renderSerializeProperty(modelInstanceVariable, propertyModel)} diff --git a/test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap b/test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap index ff361b1bf2..b5f12109a8 100644 --- a/test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap +++ b/test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap @@ -85,7 +85,7 @@ internal class TestConverter : JsonConverter } if (propertyName == \\"enumProp\\") { - var value = EnumTestExtension.ToEnumTest(JsonSerializer.Deserialize(ref reader, options)); + var value = EnumTestExtensions.ToEnumTest(JsonSerializer.Deserialize(ref reader, options)); instance.EnumProp = value; continue; }