Skip to content

JsonNamingPolicyAttribute: Add missing PascalCase - #135238

Merged
eiriktsarpalis merged 2 commits into
dotnet:mainfrom
compujuckel:stj-pascalcase-fix
Oct 6, 2026
Merged

eiriktsarpalis merged 2 commits into
dotnet:mainfrom
compujuckel:stj-pascalcase-fix

Conversation

@compujuckel

Copy link
Copy Markdown
Contributor

Support for Pascal Case was added in #124644.

However, JsonNamingPolicyAttribute was missed, leading to an exception when doing something like this:

[JsonNamingPolicy(JsonKnownNamingPolicy.PascalCase)]
public class TestClass
{
    public int Property { get; init; }
}
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'namingPolicy')
   at System.Text.Json.Serialization.JsonNamingPolicyAttribute.ResolveNamingPolicy(JsonKnownNamingPolicy namingPolicy)
   at System.Text.Json.Serialization.JsonNamingPolicyAttribute..ctor(JsonKnownNamingPolicy namingPolicy)
   at System.Reflection.CustomAttribute.<CreateCustomAttributeInstance>g____PInvoke|30_0(QCallModule __pModule_native, ObjectHandleOnStack __type_native, ObjectHandleOnStack __pCtor_native, IntPtr* __ppBlob_native, IntPtr __pEndBlob_native, Int32* __pcNamedArgs_native, ObjectHandleOnStack __instance_native)
   at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
   at System.Text.Json.Serialization.Metadata.FSharpCoreReflectionProxy.GetFSharpCoreAssembly(Type type)
   at System.Text.Json.Serialization.Metadata.FSharpCoreReflectionProxy.IsFSharpType(Type type)
   at System.Text.Json.Serialization.Converters.FSharpTypeConverterFactory.CanConvert(Type typeToConvert)
   at System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.GetBuiltInConverter(Type typeToConvert)
   at System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.GetConverterForType(Type typeToConvert, JsonSerializerOptions options, Boolean resolveJsonConverterAttribute)
   at System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.GetTypeInfo(Type type, JsonSerializerOptions options)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfoResolverChain.GetTypeInfo(Type type, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializerOptions.GetTypeInfoNoCaching(Type type)
   at System.Text.Json.JsonSerializerOptions.CachingContext.CreateCacheEntry(Type type, CachingContext context)
--- End of stack trace from previous location ---
[...]

After adding that line, the attribute works as expected.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 5, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json
See info in area-owners.md if you want to be subscribed.

@eiriktsarpalis
eiriktsarpalis enabled auto-merge (squash) October 6, 2026 18:00
@eiriktsarpalis
eiriktsarpalis merged commit 2846af4 into dotnet:main Oct 6, 2026
85 checks passed
@compujuckel
compujuckel deleted the stj-pascalcase-fix branch October 6, 2026 19:19
@eiriktsarpalis

Copy link
Copy Markdown
Member

/backport to release/11.0

@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

artl93 pushed a commit that referenced this pull request Oct 7, 2026
…5325)

Backport of #135238 to release/11.0

/cc @eiriktsarpalis @compujuckel

## Customer Impact

- [x] Customer reported
- [ ] Found internally

Fixes a customer reported issue where the newly introduced
`JsonNamingPolicyAttribute` did not include support for the newly
introduced PascalCase naming policy.

## Regression

- [ ] Yes
- [x] No

## Testing

Added functional tests verifying the fix.

## Risk

Low. Makes a targeted and straightfoward fix adding missing
functionality.

Co-authored-by: compujuckel <compujuckel@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Text.Json community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants