Skip to content

Commit

Permalink
Fix TypeMapper UseReference not used when mapping a GenericType
Browse files Browse the repository at this point in the history
  • Loading branch information
desjoerd committed Jan 8, 2025
1 parent 80a9e1a commit 814c101
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/NJsonSchema.Tests/Generation/TypeMapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,48 @@ public async Task When_generic_type_mapper_is_defined_then_it_is_called_and_the_
Assert.False(schema.Definitions.ContainsKey("MyWrapperOfMyBar"));
}

public class MyStringFoo
{
public MyWrapper<string> PropertyString { get; set; }

public MyWrapper<MyBar> PropertyMyBar { get; set; }
}

public class WithoutReferenceTypeMapper : ITypeMapper
{
public Type MappedType => typeof(MyWrapper<>);

public bool UseReference => false;

public void GenerateSchema(JsonSchema schema, TypeMapperContext context)
{
context.JsonSchemaGenerator.Generate(schema, context.Type.GenericTypeArguments[0], context.JsonSchemaResolver);
}
}


[Fact]
public async Task When_generic_type_mapper_is_defined_then_it_is_called_and_the_use_reference_false_is_used()
{
// Act
var schema = NewtonsoftJsonSchemaGenerator.FromType<MyStringFoo>(new NewtonsoftJsonSchemaGeneratorSettings
{
TypeMappers =
{
new WithoutReferenceTypeMapper()
}
});

// Assert
var json = schema.ToJson();
Assert.False(schema.Definitions.ContainsKey("string"));
Assert.False(schema.Definitions.ContainsKey("MyWrapperOfString"));
Assert.True(schema.Definitions.ContainsKey("MyBar"));

Assert.Contains("$ref", json); // The $ref to MyBar should be present
// There should only be one $ref, which is to Bar, this is the case when the first and last index of "$ref" is the same
Assert.True(json.IndexOf("$ref", StringComparison.Ordinal) == json.LastIndexOf("$ref", StringComparison.Ordinal));
}

}
}
7 changes: 7 additions & 0 deletions src/NJsonSchema/Generation/JsonTypeDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------

using System.Reflection;
using Namotion.Reflection;
using NJsonSchema.Generation.TypeMappers;

Expand Down Expand Up @@ -91,6 +92,12 @@ public static JsonTypeDescription CreateForEnumeration(ContextualType type, Json
public bool RequiresSchemaReference(IEnumerable<ITypeMapper> typeMappers)
{
var typeMapper = typeMappers.FirstOrDefault(m => m.MappedType == ContextualType.OriginalType);
if (typeMapper == null && ContextualType.OriginalType.GetTypeInfo().IsGenericType)
{
var genericType = ContextualType.OriginalType.GetGenericTypeDefinition();
typeMapper = typeMappers.FirstOrDefault(m => m.MappedType == genericType);
}

if (typeMapper != null)
{
return typeMapper.UseReference;
Expand Down

0 comments on commit 814c101

Please sign in to comment.