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
WIP: Better support for open generic when mapping types #2704
base: master
Are you sure you want to change the base?
Conversation
/// <summary> | ||
/// Provide a custom mapping, for a given type, to the Swagger-flavored JSONSchema | ||
/// </summary> | ||
/// <param name="swaggerGenOptions"></param> | ||
/// <param name="type">System type</param> | ||
/// <param name="schemaFactory">A factory method that generates Schema's for the provided type</param> | ||
public static void MapType( | ||
this SwaggerGenOptions swaggerGenOptions, | ||
Type type, | ||
Func<MappingContext, OpenApiSchema> schemaFactory) | ||
{ | ||
swaggerGenOptions.SchemaGeneratorOptions.CustomTypeMappings.Add(type, schemaFactory); | ||
} | ||
|
||
/// <summary> | ||
/// Provide a custom mapping, for a given type, to the Swagger-flavored JSONSchema | ||
/// </summary> | ||
/// <typeparam name="T">System type</typeparam> | ||
/// <param name="swaggerGenOptions"></param> | ||
/// <param name="schemaFactory">A factory method that generates Schema's for the provided type</param> | ||
public static void MapType<T>( | ||
this SwaggerGenOptions swaggerGenOptions, | ||
Func<MappingContext, OpenApiSchema> schemaFactory) | ||
{ | ||
swaggerGenOptions.MapType(typeof(T), schemaFactory); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've kept the old MapType
extension methods so that existing usages of this method won't become broken as I'm now providing the MappingContext
.
However, if anyone is using swaggerGenOptions.SchemaGeneratorOptions.CustomTypeMappings
directly instead of these helper methods, they will need to update their code.
src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/MappingContext.cs
Outdated
Show resolved
Hide resolved
...Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs
Outdated
Show resolved
Hide resolved
...Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs
Outdated
Show resolved
Hide resolved
/// <summary> | ||
/// Provide a custom mapping, for a given type, to the Swagger-flavored JSONSchema | ||
/// </summary> | ||
/// <param name="swaggerGenOptions"></param> | ||
/// <param name="type">System type</param> | ||
/// <param name="schemaFactory">A factory method that generates Schema's for the provided type</param> | ||
public static void MapType( | ||
this SwaggerGenOptions swaggerGenOptions, | ||
Type type, | ||
Func<MappingContext, OpenApiSchema> schemaFactory) | ||
{ | ||
swaggerGenOptions.SchemaGeneratorOptions.CustomTypeMappings.Add(type, schemaFactory); | ||
} | ||
|
||
/// <summary> | ||
/// Provide a custom mapping, for a given type, to the Swagger-flavored JSONSchema | ||
/// </summary> | ||
/// <typeparam name="T">System type</typeparam> | ||
/// <param name="swaggerGenOptions"></param> | ||
/// <param name="schemaFactory">A factory method that generates Schema's for the provided type</param> | ||
public static void MapType<T>( | ||
this SwaggerGenOptions swaggerGenOptions, | ||
Func<MappingContext, OpenApiSchema> schemaFactory) | ||
{ | ||
swaggerGenOptions.MapType(typeof(T), schemaFactory); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to add docs for the MappingContext
namespace Basic.Controllers | ||
{ | ||
/// <summary> | ||
/// Summary for GenericsController | ||
/// </summary> | ||
[Route("/generics")] | ||
[Produces("application/json")] | ||
public class GenericsController | ||
{ | ||
[HttpPost(Name = "CreateString")] | ||
public string CreateString([FromBody] GenericType<string> genericString) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
[HttpPost(Name = "CreateDateTime")] | ||
public string CreateDateTime([FromBody] GenericType<DateTime> genericObject) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this is the best place to put this
But it at least needs to be integration tested
src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/MappingContext.cs
Outdated
Show resolved
Hide resolved
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a test for more complex custom objects such as:
public class Foo
{
public string Bar { get; set; }
}
Thanks for contributing - if you'd like to continue with this pull request, please rebase against the default branch to pick up our new CI. |
Issues
This helps with these issues:
The problem
The issue I'm trying to solve is that there's no way to generically solve all mappings for a type that uses open generics. We can currently do something like this:
But writing an exhaustive list is impossible, especially when you need to support custom objects.
The solution
Provide the user with a
MappingContext
so they can dynamically handle different types:The
MappingContext
contains these useful properties: