Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swagger docs expose when integrating with api gateway #216

Closed
jasondaicoder opened this issue Jan 29, 2018 · 22 comments
Closed

Swagger docs expose when integrating with api gateway #216

jasondaicoder opened this issue Jan 29, 2018 · 22 comments
Labels
guidance Question that needs advice or information.

Comments

@jasondaicoder
Copy link

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

@normj
Copy link
Member

normj commented Jan 30, 2018

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

@normj normj added the Question label Jan 30, 2018
@dataneek
Copy link

I am having the same issue.

@dataneek
Copy link

Did you make any progress with this @jasondaicoder?

@rslangham
Copy link

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.

@jasondaicoder
Copy link
Author

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.

@rslangham
Copy link

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.

@jasondaicoder
Copy link
Author

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.

@rslangham
Copy link

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

@jasondaicoder
Copy link
Author

@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
Copy link
Author

@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).

@rslangham
Copy link

@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.

@MBeasM
Copy link

MBeasM commented Mar 12, 2018

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

@clouddra
Copy link

Observing the same issue as well.

@AnandKhedkar
Copy link

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.

@ghost
Copy link

ghost commented Oct 5, 2018

@AnandKhedkar that worked for me thanks

@AshwinJGitHub
Copy link

AshwinJGitHub commented Nov 3, 2018

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
Copy link

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.

@normj
Copy link
Member

normj commented Nov 7, 2018

Closing as it looks like the solution has been found.

@normj normj closed this as completed Nov 7, 2018
@diehlaws diehlaws added guidance Question that needs advice or information. and removed Question labels Jan 3, 2019
@AdityaMehtaGit
Copy link

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.

@sushfund
Copy link

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

@grahamehorner
Copy link
Contributor

I ran into the same issue; but HARDCODING PROD (the stage name) into the code is NOT AN OPTION Is there any way to identify what API gateway deployment stage a lambda is running under? a AWS environment var or something?

@ybuasen
Copy link

ybuasen commented Jul 25, 2020

@grahamehorner Here what I did to resolve the problem without hardcoding. It worked on my local machine and on Lambda

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

Basically just setup the endpoint with c.SwaggerEndpoint() using relative path url without "swagger" and leading "/".

If we supply relative path url without leading "/" to c.SwaggerEndpoint(), the absolute endpoint url will come from

AWS Serverless Url + "/" + c.RoutePrefix + "/" + the supplied url

where c.RoutePrefix is set to 'swagger' by default

For example, if your AWS Serverless Url is 'https://xyz.central.amazonaws.com/Prod', your absolute url will be 'https://xyz.central.amazonaws.com/Prod/swagger/v1/swagger.json'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
guidance Question that needs advice or information.
Projects
None yet
Development

No branches or pull requests