aspnet / Mvc

[Archived] ASP.NET Core MVC is a model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
5.61k stars 2.14k forks source link

Issue generating Clients where the controller have a route parameter, but not used in the method #8192

Closed pranavkm closed 6 years ago

pranavkm commented 6 years ago

From @syska on August 1, 2018 21:51

Not sure if this is an issue ... using the API Explorer in nswag.

The route looks like this: /api/{language}/baskets/{basketId}

language is used by aspnetcore to set the Culture

The controller looks like this:

    [Route("api/{language}/baskets")]
    public class BasketController : ApiControllerBase
    {
        private readonly IMediator _mediator;

        public BasketController(IMediator mediator)
        {
            _mediator = mediator;
        }

        [HttpGet("{basketId:guid}")]
        [ProducesResponseType(typeof(Basket), 200)]
        [ProducesResponseType(typeof(ValidationMessage[]), 400)]
        [ProducesResponseType(typeof(ForbiddenMessage), 403)]
        public async Task<IActionResult> GetBasket([FromRoute] Guid basketId)
        {
            var basket = await _mediator.Send(new GetBasket.Query(basketId));

            var newBasket = await _mediator.Send(new SaveBasket.Command(basket));

            return Json(newBasket);
        }
    }

The swagger file from swashbuckle looks like this: image

The client generated from with the API Explorer just removed the {language} parameter ... so the url ends up looking like /api/baskets/{basketId} ... and ofcause it can't hit the URL cause its missing the {language} part ...

This works if we put the {language} parameter in the method signature ... is this by design? If it is ... and I can kind a understand why ... is there any way to overcome this somehow? We need this to set the Culture ... but with a MiddlewareFilter injected on all method before route action is performed ...

Copied from original issue: RSuter/NSwag#1500

pranavkm commented 6 years ago

From @RSuter on August 1, 2018 21:55

@pranavkm is this something which is missing in api explorer?

Or should this be implemented in the AspNetCoreToSwaggerGenerator?

rynowak commented 6 years ago

Hmm, this does seem like a bug in API explorer

RicoSuter commented 6 years ago

This issue can be closed, as this was a problem in NSwag and not in API Explorer...

RicoSuter commented 6 years ago

apiParameter.Type (ApiParameterDescription) is null for these implicit parameters - NSwag now assumes typeof(string) (and not nullable for path parameters). Is this "always" correct? Or is there a way to better infer the type or maybe API Explorer should provide a type?

PR: https://github.com/RSuter/NSwag/pull/1501

pranavkm commented 6 years ago

maybe API Explorer should provide a type

The type's inferred from the type of the parameter. Presumably there's not much to infer here - outside of possibly setting it to typeof(void). Guess not assigning it just as meaningful.