5

I have an asp.net core project which uses NSwag as its Swagger tooling. The issue I am having is as follows:

  1. I have a Dto class describing the Collection entity:

#

namespace SchedulerApi.Entities
{
    public class JobCollectionDtoV2
    {
        [Required]
        [FromRoute]
        [RegularExpression("^[a-z][a-z0-9]+$")]
        [StringLength(maximumLength: 24, MinimumLength = 3)]
        public string Collection { get; set; }

        [Required]
        [FromRoute]
        [RegularExpression("^[a-z][a-z0-9]+$")]
        [StringLength(maximumLength: 24, MinimumLength = 3)]
        public string Tenant { get; set; }
    }
}
  1. The following API is used to add Collections:

#

[HttpPut]
[Route("tenants/{tenant}/collections/{collection}")]
[SwaggerResponse(typeof(JobCollectionDtoV2))]
public async Task<IActionResult> CreateTask(JobCollectionDtoV2 collectionParams)
{
}
  1. This is the Swagger file NSwag generates:

#

{
    "put": {
        "tags": [
            "JobCollectionsControllerV2"
        ],
        "operationId": "JobCollectionsControllerV2_CreateTask",
        "consumes": [
            "application/json-patch+json",
            "application/json",
            "text/json",
            "application/*+json"
        ],
        "parameters": [
            {
                "type": "string",
                "name": "Collection",
                "in": "path",
                "required": true,
                "maxLength": 24,
                "minLength": 3,
                "pattern": "^[a-z][a-z0-9]+$",
                "x-nullable": true
            },
            {
                "type": "string",
                "name": "Tenant",
                "in": "path",
                "required": true,
                "maxLength": 24,
                "minLength": 3,
                "pattern": "^[a-z][a-z0-9]+$",
                "x-nullable": true
            },
            {
                "name": "CollectionDetails",
                "in": "body",
                "required": true,
                "schema": {
                    "$ref": "#/definitions/JobCollectionDetails"
                },
                "x-nullable": false
            }
        ],
        "responses": {
            "200": {
                "x-nullable": true,
                "description": "",
                "schema": {
                    "$ref": "#/definitions/JobCollectionDtoV2"
                }
            }
        }
    }
}

Specifically the issue I am having is caused due to the fact that the path declares two variables tenant and collection with lowercase, while the DTO class declares the corresponding properties with capital case Tenant and Collection.

I am hoping to find a way to decorate the DTO class properties with an attribute that will explicitly declare the name of the parameter, something like [JsonProperty("collection")] (this particular one does not affect the output Swagger).

Appreciate your help

johni
  • 5,342
  • 6
  • 42
  • 70

0 Answers0