Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiasnordqvist committed Nov 10, 2024
1 parent 4da294c commit fa8806f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,23 @@ public static partial class Mappers
{
errors.Add(new RequiredValueMissingError([.. path, "Status"]));
}
if (mapperOptions.TryGetConverter(typeof(FartingUnicorn.Tests.SingleField.EnumType.NonNullableNonOptional_Tests.BlogPost.BlogPostStatus), out IConverter customConverter))
else if (mapperOptions.TryGetConverter(typeof(FartingUnicorn.Tests.SingleField.EnumType.NonNullableNonOptional_Tests.BlogPost.BlogPostStatus), out IConverter customConverter))
{
if (jsonStatusProperty.ValueKind != customConverter.ExpectedJsonValueKind)
{
errors.Add(new ValueHasWrongTypeError(path, customConverter.ExpectedJsonValueKind.ToString(), jsonStatusProperty.ValueKind.ToString()));
}
var result = customConverter.Convert(typeof(FartingUnicorn.Tests.SingleField.EnumType.NonNullableNonOptional_Tests.BlogPost.BlogPostStatus), jsonStatusProperty, mapperOptions, path);
if (result.Success)
{
obj.Status = result.Map(x => (FartingUnicorn.Tests.SingleField.EnumType.NonNullableNonOptional_Tests.BlogPost.BlogPostStatus)x).Value;
errors.Add(new ValueHasWrongTypeError([.. path, "Status"], customConverter.ExpectedJsonValueKind.ToString(), jsonStatusProperty.ValueKind.ToString()));
}
else
{
errors.AddRange(result.Errors.Select(x => new MappingError(path, x.Message)).ToArray());
var result = customConverter.Convert(typeof(FartingUnicorn.Tests.SingleField.EnumType.NonNullableNonOptional_Tests.BlogPost.BlogPostStatus), jsonStatusProperty, mapperOptions, [.. path, "Status"]);
if (result.Success)
{
obj.Status = result.Map(x => (FartingUnicorn.Tests.SingleField.EnumType.NonNullableNonOptional_Tests.BlogPost.BlogPostStatus)x).Value;
}
else
{
errors.AddRange(result.Errors.Select(x => new MappingError([.. path, "{p.Name}"], x.Message)).ToArray());
}
}
}
}
Expand Down
98 changes: 49 additions & 49 deletions FartingUnicorn.Tests/SingleField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -925,55 +925,55 @@ public void ValidSingleField(Func<JsonElement, Result<BlogPost>> map)
blogPost.Value.Status.Should().Be(BlogPost.BlogPostStatus.Published);
}

//[Theory]
//[MemberData(nameof(GetMappers))]
//public void MissingNonNullableField(Func<JsonElement, Result<BlogPost>> map)
//{
// var jsonElement = JsonSerializer.Deserialize<JsonElement>("""
// {
// }
// """);
// var blogPost = map(jsonElement);

// Assert.False(blogPost.Success);
// blogPost.Errors.Should().ContainSingle();
// blogPost.Errors.Single().Should().BeOfType<RequiredPropertyMissingError>();
// blogPost.Errors.Single().Message.Should().Be("$.Rating is required");
//}

//[Theory]
//[MemberData(nameof(GetMappers))]
//public void NulledNonNullableField(Func<JsonElement, Result<BlogPost>> map)
//{
// var jsonElement = JsonSerializer.Deserialize<JsonElement>("""
// {
// "Rating": null
// }
// """);
// var blogPost = map(jsonElement);

// Assert.False(blogPost.Success);
// blogPost.Errors.Should().ContainSingle();
// blogPost.Errors.Single().Should().BeOfType<RequiredValueMissingError>();
// blogPost.Errors.Single().Message.Should().Be("$.Rating must have a value");
//}

//[Theory]
//[MemberData(nameof(GetMappers))]
//public void InvalidFieldType(Func<JsonElement, Result<BlogPost>> map)
//{
// var jsonElement = JsonSerializer.Deserialize<JsonElement>("""
// {
// "Rating": true
// }
// """);
// var blogPost = map(jsonElement);

// Assert.False(blogPost.Success);
// blogPost.Errors.Should().ContainSingle();
// blogPost.Errors.Single().Should().BeOfType<ValueHasWrongTypeError>();
// blogPost.Errors.Single().Message.Should().Be("Value of $.Rating has the wrong type. Expected Number, got True");
//}
[Theory]
[MemberData(nameof(GetMappers))]
public void MissingNonNullableField(Func<JsonElement, Result<BlogPost>> map)
{
var jsonElement = JsonSerializer.Deserialize<JsonElement>("""
{
}
""");
var blogPost = map(jsonElement);

Assert.False(blogPost.Success);
blogPost.Errors.Should().ContainSingle();
blogPost.Errors.Single().Should().BeOfType<RequiredPropertyMissingError>();
blogPost.Errors.Single().Message.Should().Be("$.Status is required");
}

[Theory]
[MemberData(nameof(GetMappers))]
public void NulledNonNullableField(Func<JsonElement, Result<BlogPost>> map)
{
var jsonElement = JsonSerializer.Deserialize<JsonElement>("""
{
"Status": null
}
""");
var blogPost = map(jsonElement);

Assert.False(blogPost.Success);
blogPost.Errors.Should().ContainSingle();
blogPost.Errors.Single().Should().BeOfType<RequiredValueMissingError>();
blogPost.Errors.Single().Message.Should().Be("$.Status must have a value");
}

[Theory]
[MemberData(nameof(GetMappers))]
public void InvalidFieldType(Func<JsonElement, Result<BlogPost>> map)
{
var jsonElement = JsonSerializer.Deserialize<JsonElement>("""
{
"Status": true
}
""");
var blogPost = map(jsonElement);

Assert.False(blogPost.Success);
blogPost.Errors.Should().ContainSingle();
blogPost.Errors.Single().Should().BeOfType<ValueHasWrongTypeError>();
blogPost.Errors.Single().Message.Should().Be("Value of $.Status has the wrong type. Expected String, got True");
}
}

//public class NullableNonOptional_Tests
Expand Down
22 changes: 13 additions & 9 deletions MapperGenerator/MapperGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,28 @@ public static SourceText GenerateExtensionClass(ClassToGenerateMapperFor classTo
}
else
{
sb.AppendLine($"if (mapperOptions.TryGetConverter(typeof({p.Type}), out IConverter customConverter))");
sb.AppendLine($"else if (mapperOptions.TryGetConverter(typeof({p.Type}), out IConverter customConverter))");
using(var _4 = sb.CodeBlock())
{
sb.AppendLine($"if (json{p.Name}Property.ValueKind != customConverter.ExpectedJsonValueKind)");
using(var _5 = sb.CodeBlock())
{
sb.AppendLine($"errors.Add(new ValueHasWrongTypeError(path, customConverter.ExpectedJsonValueKind.ToString(), json{p.Name}Property.ValueKind.ToString()));");
}
sb.AppendLine($"var result = customConverter.Convert(typeof({p.Type}), json{p.Name}Property, mapperOptions, path);");
sb.AppendLine("if (result.Success)");
using(var _5 = sb.CodeBlock())
{
sb.AppendLine($"obj.{p.Name} = result.Map(x => ({p.Type})x).Value;");
sb.AppendLine($"errors.Add(new ValueHasWrongTypeError([.. path, \"{p.Name}\"], customConverter.ExpectedJsonValueKind.ToString(), json{p.Name}Property.ValueKind.ToString()));");
}
sb.AppendLine("else");
using (var _5 = sb.CodeBlock())
{
sb.AppendLine("errors.AddRange(result.Errors.Select(x => new MappingError(path, x.Message)).ToArray());");
sb.AppendLine($"var result = customConverter.Convert(typeof({p.Type}), json{p.Name}Property, mapperOptions, [.. path, \"{p.Name}\"]);");
sb.AppendLine("if (result.Success)");
using (var _6 = sb.CodeBlock())
{
sb.AppendLine($"obj.{p.Name} = result.Map(x => ({p.Type})x).Value;");
}
sb.AppendLine("else");
using (var _6 = sb.CodeBlock())
{
sb.AppendLine("errors.AddRange(result.Errors.Select(x => new MappingError([.. path, \"{p.Name}\"], x.Message)).ToArray());");
}
}
}
}
Expand Down

0 comments on commit fa8806f

Please sign in to comment.