Skip to content

Commit c1cfc9e

Browse files
Copilotcaptainsafia
andcommitted
Address review feedback: Add null check in FormatComplexKey and test for JsonPropertyName attributes
Co-authored-by: captainsafia <1857993+captainsafia@users.noreply.github.com>
1 parent 193c6a7 commit c1cfc9e

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

src/Http/Http.Abstractions/src/Validation/ValidateContext.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ private string FormatComplexKey(string key)
134134
int lastIndex = 0;
135135
int i = 0;
136136
bool inBracket = false;
137+
var propertyNamingPolicy = SerializerOptions?.PropertyNamingPolicy;
137138

138139
while (i < key.Length)
139140
{
@@ -145,7 +146,9 @@ private string FormatComplexKey(string key)
145146
if (i > lastIndex)
146147
{
147148
string segment = key.Substring(lastIndex, i - lastIndex);
148-
string formattedSegment = SerializerOptions!.PropertyNamingPolicy!.ConvertName(segment);
149+
string formattedSegment = propertyNamingPolicy != null
150+
? propertyNamingPolicy.ConvertName(segment)
151+
: segment;
149152
result.Append(formattedSegment);
150153
}
151154

@@ -172,7 +175,9 @@ private string FormatComplexKey(string key)
172175
if (i > lastIndex)
173176
{
174177
string segment = key.Substring(lastIndex, i - lastIndex);
175-
string formattedSegment = SerializerOptions!.PropertyNamingPolicy!.ConvertName(segment);
178+
string formattedSegment = propertyNamingPolicy != null
179+
? propertyNamingPolicy.ConvertName(segment)
180+
: segment;
176181
result.Append(formattedSegment);
177182
}
178183
result.Append(c);
@@ -186,9 +191,9 @@ private string FormatComplexKey(string key)
186191
if (lastIndex < key.Length)
187192
{
188193
string segment = key.Substring(lastIndex);
189-
if (!inBracket)
194+
if (!inBracket && propertyNamingPolicy != null)
190195
{
191-
segment = SerializerOptions!.PropertyNamingPolicy!.ConvertName(segment);
196+
segment = propertyNamingPolicy.ConvertName(segment);
192197
}
193198
result.Append(segment);
194199
}

src/Http/Http.Abstractions/test/Validation/ValidatableTypeInfoTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,12 +857,62 @@ private class PersonWithJsonNames
857857
public string? LastName { get; set; }
858858
}
859859

860+
[Fact]
861+
public async Task Validate_RespectsJsonPropertyNameAttribute_ForValidationErrors()
862+
{
863+
// Arrange
864+
var modelType = new TestValidatableTypeInfo(
865+
typeof(ModelWithJsonPropertyNames),
866+
[
867+
CreatePropertyInfo(typeof(ModelWithJsonPropertyNames), typeof(string), "UserName", "UserName",
868+
[new RequiredAttribute()]),
869+
CreatePropertyInfo(typeof(ModelWithJsonPropertyNames), typeof(string), "EmailAddress", "EmailAddress",
870+
[new EmailAddressAttribute()])
871+
]);
872+
873+
var model = new ModelWithJsonPropertyNames { EmailAddress = "invalid-email" }; // Missing username and invalid email
874+
875+
var jsonOptions = new JsonSerializerOptions();
876+
// Add a custom converter that knows about JsonPropertyName attributes
877+
jsonOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
878+
879+
var context = new ValidateContext
880+
{
881+
ValidationOptions = new TestValidationOptions(new Dictionary<Type, ValidatableTypeInfo>
882+
{
883+
{ typeof(ModelWithJsonPropertyNames), modelType }
884+
}),
885+
ValidationContext = new ValidationContext(model),
886+
SerializerOptions = jsonOptions
887+
};
888+
889+
// Act
890+
await modelType.ValidateAsync(model, context, default);
891+
892+
// Assert
893+
Assert.NotNull(context.ValidationErrors);
894+
Assert.Collection(context.ValidationErrors,
895+
kvp =>
896+
{
897+
// Currently uses camelCase naming policy, not JsonPropertyName
898+
Assert.Equal("userName", kvp.Key);
899+
Assert.Equal("The UserName field is required.", kvp.Value.First());
900+
},
901+
kvp =>
902+
{
903+
// Currently uses camelCase naming policy, not JsonPropertyName
904+
Assert.Equal("emailAddress", kvp.Key);
905+
Assert.Equal("The EmailAddress field is not a valid e-mail address.", kvp.Value.First());
906+
});
907+
}
908+
860909
private class ModelWithJsonPropertyNames
861910
{
862911
[JsonPropertyName("username")]
863912
public string? UserName { get; set; }
864913

865914
[JsonPropertyName("email")]
915+
[EmailAddress]
866916
public string? EmailAddress { get; set; }
867917
}
868918

0 commit comments

Comments
 (0)