Swashbuckle.AspNetCore
Swashbuckle.AspNetCore copied to clipboard
ModelMetadataType attributes are ignored if the target class is inherited
Version="6.4.0" Steps to reproduce:
- Extend a base DTO and use metadata attributes to differentiate DTOs for different actions.
- Reference inherited classes and generate Swagger.json
public class BaseDto
{
public Guid Id { get; set; }
public Guid ProductId { get; set; }
[StringLength(255)]
public string? Name { get; set; }
}
[ModelMetadataType(typeof(CreateDtoMetadata))]
public class CreateDto : BaseDto { }
[ModelMetadataType(typeof(UpdateDtoMetadata))]
public class UpdateDto : BaseDto { }
public class CreateDtoMetadata
{
[Required]
public Guid ProductId { get; set; }
[Required]
public string? Name { get; set; }
}
public class UpdateDtoMetadata
{
[Required]
public Guid Id { get; set; }
[Required]
public string? Name { get; set; }
}
Expected result: In generated swagger.json both CreateDto and UpdateDto schemas should be different and have the combination of the attributes from Base DTO class and from corresponded metadata classes:
"CreateDto": {
"required": ["name","productId"],
"type": "object",
"properties": {
"id": {"type": "string", "format": "uuid"},
"productId": {"type": "string", "format": "uuid"},
"name": {"maxLength": 255,"minLength": 0,"type": "string"}
},"additionalProperties": false}
"UpdateDto": {
"required": ["id","name"],
"type": "object",
"properties": {"id": {"type": "string","format": "uuid"},
"productId": {"type": "string","format": "uuid"},
"name": {"maxLength": 255,"minLength": 0,"type": "string"}
},
"additionalProperties": false
},
Actual result: both inherited classes are identical, attributes from metadata classes are ignored
"CreateDto": {
"type": "object",
"properties": {
"id": {"type": "string","format": "uuid"},
"productId": {"type": "string","format": "uuid"},
"name": {"maxLength": 255,"minLength": 0,"type": "string", "nullable": true}
},
"additionalProperties": false
},
This issue is stale because it has been open for 60 days with no activity. It will be automatically closed in 14 days if no further updates are made.
Looks like this might be a duplicate of #1981.
This issue is stale because it has been open for 60 days with no activity. It will be automatically closed in 14 days if no further updates are made.
This issue was closed because it has been inactive for 14 days since being marked as stale.