diff --git a/examples/csharp-generate-newtonsoft-serializer/__snapshots__/index.spec.ts.snap b/examples/csharp-generate-newtonsoft-serializer/__snapshots__/index.spec.ts.snap index 86305294c3..f70ff62271 100644 --- a/examples/csharp-generate-newtonsoft-serializer/__snapshots__/index.spec.ts.snap +++ b/examples/csharp-generate-newtonsoft-serializer/__snapshots__/index.spec.ts.snap @@ -19,7 +19,6 @@ public partial class Root get { return name; } set { this.name = value; } } - public string Serialize() { return JsonConvert.SerializeObject(this); diff --git a/src/generators/csharp/presets/NewtonsoftSerializerPreset.ts b/src/generators/csharp/presets/NewtonsoftSerializerPreset.ts index 611a568345..bad6cea505 100644 --- a/src/generators/csharp/presets/NewtonsoftSerializerPreset.ts +++ b/src/generators/csharp/presets/NewtonsoftSerializerPreset.ts @@ -87,33 +87,32 @@ function renderDeserialize({ !(prop.property instanceof ConstrainedDictionaryModel) || prop.property.serializationType === 'normal' ); - const corePropsRead = coreProps - .map((prop) => { - const propertyAccessor = pascalCase(prop.propertyName); - let toValue = `jo["${prop.unconstrainedPropertyName}"].ToObject<${prop.property.type}>(serializer)`; - if ( - prop.property instanceof ConstrainedReferenceModel && - prop.property.ref instanceof ConstrainedEnumModel - ) { - toValue = `${prop.property.name}Extensions.To${prop.property.name}(jo["${ - prop.unconstrainedPropertyName - }"].ToString())${prop.required ? '.Value' : ''}`; - } - if ( - options?.enforceRequired !== undefined && - options?.enforceRequired && - prop.required - ) { - return `if(jo["${prop.unconstrainedPropertyName}"] is null){ + const corePropsRead = coreProps.map((prop) => { + const propertyAccessor = pascalCase(prop.propertyName); + let toValue = `jo["${prop.unconstrainedPropertyName}"].ToObject<${prop.property.type}>(serializer)`; + if ( + prop.property instanceof ConstrainedReferenceModel && + prop.property.ref instanceof ConstrainedEnumModel + ) { + toValue = `${prop.property.name}Extensions.To${prop.property.name}(jo["${ + prop.unconstrainedPropertyName + }"].ToString())${prop.required ? '.Value' : ''}`; + } + + if ( + options?.enforceRequired !== undefined && + options?.enforceRequired && + prop.required + ) { + return `if(jo["${prop.unconstrainedPropertyName}"] is null){ throw new JsonSerializationException("Required property '${prop.unconstrainedPropertyName}' is missing"); } - value.${propertyAccessor} = ${toValue}; `; - } + } - return `if(jo["${prop.unconstrainedPropertyName}"] != null) { + return `if(jo["${prop.unconstrainedPropertyName}"] != null) { value.${propertyAccessor} = ${toValue}; }`; }) diff --git a/test/generators/csharp/presets/__snapshots__/NewtonsoftSerializerPreset.spec.ts.snap b/test/generators/csharp/presets/__snapshots__/NewtonsoftSerializerPreset.spec.ts.snap index 816b7c4208..870264de96 100644 --- a/test/generators/csharp/presets/__snapshots__/NewtonsoftSerializerPreset.spec.ts.snap +++ b/test/generators/csharp/presets/__snapshots__/NewtonsoftSerializerPreset.spec.ts.snap @@ -93,6 +93,7 @@ if(jo[\\"objectProp\\"] != null) { } var additionalProperties = jo.Properties().Where((prop) => prop.Name != \\"string prop\\" || prop.Name != \\"const string prop\\" || prop.Name != \\"notRequiredStringProp\\" || prop.Name != \\"numberProp\\" || prop.Name != \\"enumProp\\" || prop.Name != \\"objectProp\\"); + value.AdditionalProperties = new Dictionary(); foreach (var additionalProperty in additionalProperties)