-
Notifications
You must be signed in to change notification settings - Fork 5.3k
JSON support required properties #73063
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ private sealed partial class Emitter | |
| private const string DefaultContextBackingStaticVarName = "s_defaultContext"; | ||
| internal const string GetConverterFromFactoryMethodName = "GetConverterFromFactory"; | ||
| private const string InfoVarName = "info"; | ||
| private const string PropertyInfoVarName = "propertyInfo"; | ||
| internal const string JsonContextVarName = "jsonContext"; | ||
| private const string NumberHandlingPropName = "NumberHandling"; | ||
| private const string ObjectCreatorPropName = "ObjectCreator"; | ||
|
|
@@ -709,6 +710,7 @@ private static string GeneratePropMetadataInitFunc(TypeGenerationSpec typeGenera | |
| string memberTypeCompilableName = memberTypeMetadata.TypeRef; | ||
|
|
||
| string infoVarName = $"{InfoVarName}{i}"; | ||
| string propertyInfoVarName = $"{PropertyInfoVarName}{i}"; | ||
|
|
||
| sb.Append($@" | ||
| {JsonPropertyInfoValuesTypeRef}<{memberTypeCompilableName}> {infoVarName} = new {JsonPropertyInfoValuesTypeRef}<{memberTypeCompilableName}>() | ||
|
|
@@ -728,7 +730,16 @@ private static string GeneratePropMetadataInitFunc(TypeGenerationSpec typeGenera | |
| JsonPropertyName = {jsonPropertyNameValue} | ||
| }}; | ||
|
|
||
| {PropVarName}[{i}] = {JsonMetadataServicesTypeRef}.CreatePropertyInfo<{memberTypeCompilableName}>({OptionsLocalVariableName}, {infoVarName}); | ||
| {JsonPropertyInfoTypeRef} {propertyInfoVarName} = {JsonMetadataServicesTypeRef}.CreatePropertyInfo<{memberTypeCompilableName}>({OptionsLocalVariableName}, {infoVarName});"); | ||
|
|
||
| if (memberMetadata.IsRequired) | ||
| { | ||
| sb.Append($@" | ||
| {propertyInfoVarName}.IsRequired = true;"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we wish to set other applicable properties directly on
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally speaking we could probably get rid of most of the usages of JsonMetadataServices but that might be a bit more work since we'd have to refactor also JsonTypeInfo part. While I could start doing it gradually it probably makes more sense to do it in one go but for that we need to be able to customize parameters which currently contract customization doesn't support. The reason this one is separate is because it doesn't have API in JsonMetadataServices but because we can avoid adding such API I went ahead and did it this way. I'll keep changes in this PR to minimum, we can revisit this once we get full parameters support |
||
| } | ||
|
|
||
| sb.Append($@" | ||
| {PropVarName}[{i}] = {propertyInfoVarName}; | ||
| "); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Text.Json.Serialization.Metadata; | ||
|
|
||
| namespace System.Text.Json.Serialization | ||
| { | ||
| /// <summary> | ||
| /// Indicates that the annotated member must bind to a JSON property on deserialization. | ||
| /// </summary> | ||
krwq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// <remarks> | ||
| /// <see langword="null"/> token in JSON will not trigger a validation error. | ||
| /// For contracts originating from <see cref="DefaultJsonTypeInfoResolver"/> or <see cref="JsonSerializerContext"/>, | ||
| /// this attribute will be mapped to <see cref="JsonPropertyInfo.IsRequired"/>. | ||
| /// </remarks> | ||
| [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] | ||
krwq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public sealed class JsonRequiredAttribute : JsonAttribute | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of <see cref="JsonRequiredAttribute"/>. | ||
| /// </summary> | ||
| public JsonRequiredAttribute() { } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't the default constructor be removed here? Or is it required by ApiCompat?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's not, I followed existing pattern with JsonInclude. We can put better xml doc comment I guess |
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.