Skip to content
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

Examples are not added to generic types for Swagger v2 #200

Open
ouvreboite opened this issue Mar 24, 2022 · 1 comment
Open

Examples are not added to generic types for Swagger v2 #200

ouvreboite opened this issue Mar 24, 2022 · 1 comment

Comments

@ouvreboite
Copy link

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;
        }
    }
}
@mattfrear
Copy link
Owner

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:
image

But it sounds like you have a workaround, with "a custom SchemaIdSelector". How do you do that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants