aws-lambda-dotnet icon indicating copy to clipboard operation
aws-lambda-dotnet copied to clipboard

Swagger docs expose when integrating with api gateway

Open jasondaicoder opened this issue 8 years ago • 22 comments

Hi guys,

I have a .net core web api which has its own swagger integration docs exposed via "swagger/v1/swagger.json" endpoint. It's deployed as lambda. I also have set up a api gateway which integrates with this lambda to expose it's all apis.

The problem I have is I can't access the swagger docs via the /swagger url. It always returns "message: forbibbden". I can definitely access the swagger if I run the web api locally.

Any ideas?

Thanks

Jason

jasondaicoder avatar Jan 29 '18 23:01 jasondaicoder

Can you create a simple repo repository for me to take a look at?

normj avatar Jan 30 '18 02:01 normj

I am having the same issue.

dataneek avatar Feb 27 '18 18:02 dataneek

Did you make any progress with this @jasondaicoder?

dataneek avatar Feb 27 '18 19:02 dataneek

Same problem, please update if you find a fix. I am able to access the json directly, e.g. .. https://...../Prod/swagger/v1/swagger.json . Since API Gateway supports swagger, was thinking something magic about the "swagger" path. But, using the info from this page, http://eatcodelive.com/2017/05/19/change-default-swagger-route-in-an-asp-net-core-web-api/ , I changed it to "swag" and still had the same problem.

Interesting, when you access https://..../Prod/swagger it redirects you to https://.../swagger, basically removing the stage part of the path and back to the root, which then results in the message forbidden.

rslangham avatar Mar 01 '18 20:03 rslangham

Not really guys. I thought it could be because api gateway is supposed to act for web api only but not for UI related. I ended up with hosting the UI somewhere else but pointing to the swagger json file of the project through api gateway. It works.

jasondaicoder avatar Mar 01 '18 21:03 jasondaicoder

Thanks. Hmm, kinda pain to have to do this :) I do have a regular MVC home controller and Index page as part of my deploy and it works fine. Yep, I was just reading on a post where a person thought may be related to where the response was not json, but my Index page works fine.

rslangham avatar Mar 01 '18 21:03 rslangham

With my solution you actually only need to deploy the UI once. As long as you load the json file from your project you don’t need to do anything anymore. We host the UI files on S3. Maybe AWS team can fix the issue later.

jasondaicoder avatar Mar 01 '18 21:03 jasondaicoder

interesting.. you can deploy a .net core ui on S3 without a running kestrel? figured the swagger was kinda a runtime thing and needed to support the running dlls etc, e.g. not just a static set of files

rslangham avatar Mar 01 '18 21:03 rslangham

@normj, the project is just a normal c# weapi with Lamdba entry point. Nothing really special. I will try to create a sample repository on GitHub later. I don’t think anything wrong with my project to be honest as other people are having the same issue. I think it could be some conflicts with api gateway builtin swagger docs.

jasondaicoder avatar Mar 01 '18 21:03 jasondaicoder

@rslangham no you don’t need to deploy your project. All you need is to download the swagger ui static files which you can get from swagger site ( maybe a link to GitHub which you can find on swagger site).

jasondaicoder avatar Mar 01 '18 21:03 jasondaicoder

@jasondaicoder thanks for the suggestion on using the swagger ui static files. I did this using a different implementation and it worked. I downloaded the swagger ui files and put them under my project wwwroot/swaggerui. In the Startup.cs Configure(..) I enabled static files access, e.g. app.UseStaticFiles. I modified the swagger ui index.html embedded javascript to use window.location.href to dynamically change the url to the correct path for the swagger json, this allows it to be deployed anywhere and work.

Really would like to find out the correct solution though :) Thanks again.

rslangham avatar Mar 02 '18 20:03 rslangham

Hi there, I'm having the exact same issue, I hope it's resolved soon.

MBeasM avatar Mar 12 '18 11:03 MBeasM

Observing the same issue as well.

clouddra avatar Mar 24 '18 17:03 clouddra

I was also having the same problem. Got solution- Please make sure you have added the stage in a name in startup file as shown below. where Prod is my stage name.

app.UseSwaggerUI(c =>
              {
                  c.SwaggerEndpoint("/Prod/swagger/v1/swagger.json", "GST Console API - V1");

              });

Requesting @normj to close this.

AnandKhedkar avatar Jun 28 '18 09:06 AnandKhedkar

@AnandKhedkar that worked for me thanks

ghost avatar Oct 05 '18 12:10 ghost

I was also having the same problem. Got solution- Please make sure you have added the stage in a name in startup file as shown below. where Prod is my stage name.

app.UseSwaggerUI(c =>
              {
                  c.SwaggerEndpoint("/Prod/swagger/v1/swagger.json", "GST Console API - V1");

              });

Requesting @normj to close this.

That does show up the Index.html page. However, it doesn't allow execution of the service through the swagger interface for me. The path in the Request URL for a GET call excludes Prod again. I am however, using the c.RoutePrefix = string.Empty; to render the page at the root instead of adding /swagger.

Is there a property that could be set so that this will stay consistent across all the relative url references?

AshwinJGitHub avatar Nov 03 '18 16:11 AshwinJGitHub

I was also having the same problem. Got solution- Please make sure you have added the stage in a name in startup file as shown below. where Prod is my stage name.

app.UseSwaggerUI(c =>
              {
                  c.SwaggerEndpoint("/Prod/swagger/v1/swagger.json", "GST Console API - V1");

              });

Requesting @normj to close this.

That does show up the Index.html page. However, it doesn't allow execution of the service through the swagger interface for me. The path in the Request URL for a GET call excludes Prod again. I am however, using the c.RoutePrefix = string.Empty; to render the page at the root instead of adding /swagger.

Is there a property that could be set so that this will stay consistent across all the relative url references?

Resolved with this solution Works perfectly and even shows the base path in the header of the swagger page.

AshwinJGitHub avatar Nov 03 '18 18:11 AshwinJGitHub

Closing as it looks like the solution has been found.

normj avatar Nov 07 '18 00:11 normj

I was also having the same problem. Got solution- Please make sure you have added the stage in a name in startup file as shown below. where Prod is my stage name.

app.UseSwaggerUI(c =>
              {
                  c.SwaggerEndpoint("/Prod/swagger/v1/swagger.json", "GST Console API - V1");

              });

Requesting @normj to close this. In which file to define this.

AdityaMehtaGit avatar Dec 26 '19 08:12 AdityaMehtaGit

I was also having the same problem. Got solution- Please make sure you have added the stage in a name in startup file as shown below. where Prod is my stage name.

app.UseSwaggerUI(c =>
              {
                  c.SwaggerEndpoint("/Prod/swagger/v1/swagger.json", "GST Console API - V1");

              });

Requesting @normj to close this.

this worked for me. Thanks

sushfund avatar Feb 25 '20 16:02 sushfund