Skip to content

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

Open
@ouvreboite

Description

@ouvreboite

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;
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions