steve-jansen / Swagger.Net

Library to document the ASP.NET Web API using the Swagger specification
0 stars 3 forks source link

Swagger should use the controller name for each apis/path value #1

Open steve-jansen opened 11 years ago

steve-jansen commented 11 years ago

Overview

Swagger.Net incorrectly uses the path to the first action in a controller as the top level name of the API. We should just use the name of the controller (or shortest URL path without the querystring for non-default routes.)

Steps To Reproduce

  1. Start the Swagger.Net.WebAPI project in VS
  2. Navigate to http://localhost:####/api/swagger

Actual Results The first api for the "Pet" controller is listed as path: "/api/docs/Pet?page={page}&size={size}"

Note that the {} chars in the name breaks the version of Swagger UI packaged with Swagger.Net when trying to load the details for the controller via http://localhost:65358/api/docs/Pet?page={page}&size={size}. The ideal URL would be http://localhost:65358/api/docs/Pet, which currently works.

Expected Results The api for the "Pet" controller should be path: "/api/docs/Pet"

steve-jansen commented 11 years ago

See commit f3bd3b6ba917 for the repro above.

mknayak commented 11 years ago

Yes, we need to use only path not the path&query. I changed the CreateResourceApi to include a flag swaggerRoot. if request is to generate documentation for all apis with in a controller, we need to call with swaggerRoot=true which will trim down the querystring part. But for ControllerActions we need to provide complete path+querystring.

 public static ResourceApi CreateResourceApi(ApiDescription api,bool swaggerRoot)
        {
            var apiRelPath = api.RelativePath;
            if (swaggerRoot)
            {
                int queryIndex = apiRelPath.IndexOf('?');
                if (queryIndex > 0)
                {
                    apiRelPath = apiRelPath.Substring(0, queryIndex);
                }
            }
            ResourceApi rApi = new ResourceApi()
            {
                path = "/" + apiRelPath,
                description = api.Documentation,
                operations = new List<ResourceApiOperation>()
            };

            return rApi;
        }

image