0

I have an aspnet core project with a simple REST API.

NSwag is used as Swagger tool, and it works based on the decorations I put on the models and controller's methods:

[Route("api/v2/")] public class JobCollectionsControllerV2 : Controller { [HttpPut] [Route("tenants/{tenant}/collections/{collection}")] [SwaggerResponse(typeof(JobCollectionDtoV2))] public async Task<IActionResult> CreateTask(JobCollectionDtoV2 collectionParams) { // removed } } 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; } [Required] [FromBody] public JobCollectionDetails CollectionDetails { get; set; } public override string ToString() { return JsonConvert.SerializeObject(this); } } public class JobCollectionDetails { [Required] public bool Enabled { get; set; } [Required] [CollectionFrequency] public TimeSpan Frequency { get; set; } } 

The above code makes NSwag generate the following Swagger file:

{ "put": { "tags": [ "JobCollectionsControllerV2" ], "operationId": "JobCollectionsControllerV2_CreateTask", "parameters": [ { "type": "string", "name": "collection", "in": "path", "required": true, "maxLength": 24, "minLength": 3, "pattern": "^[a-z][a-z0-9]+$", "x-nullable": false }, { "type": "string", "name": "tenant", "in": "path", "required": true, "maxLength": 24, "minLength": 3, "pattern": "^[a-z][a-z0-9]+$", "x-nullable": false }, { "type": "object", "name": "collectionDetails", "in": "query", "required": true, "x-schema": { "$ref": "#/definitions/JobCollectionDetails" }, "x-nullable": true } ], "responses": { "200": { "x-nullable": true, "description": "", "schema": { "$ref": "#/definitions/JobCollectionDtoV2" } } } } } 

Looks great, except for the following part which specifies that the collectionDetails should come from the query parameters and not from the body.

 { "type": "object", "name": "collectionDetails", "in": "query", <<<<-------------- SHOULD BE 'body' or something like that "required": true, "x-schema": { "$ref": "#/definitions/JobCollectionDetails" }, "x-nullable": true } 

I am not sure how I can fix this -- I really appreciate your help on this.

Thank you!

EDIT #1 (NSwag initialization):

 public void ConfigureServices(IServiceCollection services) { if (_env.IsDevelopment()) { services.AddMvc(); services.AddSwaggerDocument(); } } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { _loggerFactory.AddDebug(); app.UseDeveloperExceptionPage(); app.UseSwagger(settings => { settings.PostProcess = (document, request) => { document.Info.Version = _context.CodePackageActivationContext.CodePackageVersion; document.Info.TermsOfService = "None"; document.Info.Contact = new SwaggerContact { }; }; }); app.UseSwaggerUi3(); } } 
2
  • Can you show how the middleware is registered in Startup.cs Commented Jan 14, 2019 at 7:37
  • 1
    I have updated to NSwag v12 and the issue was resolved Commented Jan 14, 2019 at 12:59

1 Answer 1

4

I have updated the NSwag libraries to latest v12 (from v11) and the issue was resolved -- the query was indeed replaced by body

No other changes were made.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.