Examples are not added to generic types for Swagger v2
ouvreboite opened this issue · 1 comments
Swashbuckle.AspNetCore.Filter 7.0.2
When adding an example with an IExamplesProvider for a generic type, the example does no appear in the generated Swagger v2.
public class BoostingRequestExample : IExamplesProvider<ValueResourceInput<BoostingConfigurationRequest>>
{
public ValueResourceInput<BoostingConfigurationRequest> GetExamples()
{
return new ValueResourceInput<BoostingConfigurationRequest>
(new BoostingConfigurationRequest { boostingFactor = 1.0});
}
}
It seems due to TypeExtensions.SchemaDefinitionName not using Swashbuckle's SchemaIdSelector.
For ValueResourceInput<BoostingConfigurationRequest>
, SchemaDefinitionName returns BoostingConfigurationRequestValueResourceInput, while Swashbuckle names it ValueResourceInput[BoostingConfigurationRequest]
If I create a custom SchemaIdSelector to have Swashbuckle generate the id the same way as TypeExtensions.SchemaDefinitionName, it works.
// from RequestExample.SetRequestExampleForOperation
if (swaggerOptions.SerializeAsV2)
{
// Swagger v2 doesn't have a request example on the path
// Fallback to setting it on the object in the "definitions"
string schemaDefinitionName = requestType.SchemaDefinitionName();
if (schemaRepository.Schemas.ContainsKey(schemaDefinitionName))
{
//HERE: the actual schema name in the repository don't match the one from SchemaDefinitionName(), so the example is not added
var schemaDefinition = schemaRepository.Schemas[schemaDefinitionName];
if (schemaDefinition.Example == null)
{
schemaDefinition.Example = firstOpenApiExample;
}
}
}
Bonjour Jean-Baptiste,
My first question is - do you need to use my filter to add examples? Or can you use XML comments instead?
https://github.com/domaindrivendev/Swashbuckle.AspNetCore#include-descriptions-from-xml-comments
This is the sort of edge case I have trouble supporting. You're using v2 which is not common, and generics examples which is also uncommon. I have a test case in my test project which works fine - I just tested it with v2:
But it sounds like you have a workaround, with "a custom SchemaIdSelector". How do you do that?