Swashbuckle.AspNetCore icon indicating copy to clipboard operation
Swashbuckle.AspNetCore copied to clipboard

ModelMetadataType attributes are ignored if the target class is inherited

Open whitebehemoth opened this issue 2 years ago • 2 comments

Version="6.4.0" Steps to reproduce:

  1. Extend a base DTO and use metadata attributes to differentiate DTOs for different actions.
  2. 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
},

whitebehemoth avatar Sep 26 '22 19:09 whitebehemoth

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.

github-actions[bot] avatar Apr 16 '24 01:04 github-actions[bot]

Looks like this might be a duplicate of #1981.

martincostello avatar Apr 16 '24 08:04 martincostello

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.

github-actions[bot] avatar Jun 16 '24 01:06 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Jul 03 '24 01:07 github-actions[bot]