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

Prefix missing from Swagger #820

Closed
IngoVals opened this issue Mar 27, 2024 · 7 comments
Closed

Prefix missing from Swagger #820

IngoVals opened this issue Mar 27, 2024 · 7 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@IngoVals
Copy link

Run counterfact with --prefix /something

/something prefix is missing from Swagger

@dethell
Copy link
Collaborator

dethell commented Mar 27, 2024

Can you elaborate here? Are you saying if the OpenApi spec is missing a prefix you want Counterfact to add it?

@IngoVals
Copy link
Author

I haven't given this much thought. But as I understand it OpenAPI is relative to some basepath.

In most cases this basepath is just the domain

https://myfancyapi.com/

But in some cases it could be relevant to a prefixed url

https://bigcorp.com/employeeGateway/

As I understand it openAPI will not mention this employeeGateway in any way, all paths are just appended to the end of the url.

For Counterfact, the basepath is by defauly localhost:3100 so our paths will be added to it. Say our internal api paths are /api/person/1 the resulting url will be http://localhost:3100/api/person/1

When I start up Counterfact I can add a prefix, using --prefix So if I add /employeeGateway Counterfact is available at

http://localhost:3100/employeeGateway/api/person/1

This will ensure that I can more easily change from a production and mock api on a running client.

However if I go into Swagger and try out I get 404 because swagger tries out the normal path.

@dethell
Copy link
Collaborator

dethell commented Mar 27, 2024

A recent PR updated CF to support the basePath: #797

So now if your spec has a basePath included, and if your client includes that path in its requests, Counterfact will silently handle it correctly. Let me know if that meets this need.

@pmcelhaney
Copy link
Owner

This will ensure that I can more easily change from a production and mock api on a running client.

There's a better way. You can have Counterfact proxy to the production server. You can then switch between mocks and the proxy to production at runtime. The client code doesn't need a mechanism to switch back and forth at runtime.

I have big plans to build on this feature. You'll be able to have some endpoints point to production while others are mocked. You'll be able to flip the switch at runtime through a GUI that also serves as an alternative to Postman or Swagger UI which is easier to use.

I'm typing on my phone so I can't easily link to the docs, but please let me know if that makes sense and fits your use case.

@pmcelhaney
Copy link
Owner

@IngoVals What do you think? Do you still have concerns or can we close this issue?

@IngoVals
Copy link
Author

IngoVals commented Apr 5, 2024

What about the prefix attribute in Counterfact, if the plan is to support that still you could just try it out. Run counterfact, with a prefix, open up the swagger endpoint, and try to run an endpoint.

Unless I'm missing something this will not work, which is probably not what we want.

@pmcelhaney pmcelhaney added the good first issue Good for newcomers label Apr 5, 2024
@pmcelhaney
Copy link
Owner

You're right! I forgot that before the OpenAPI doc is passed to Swagger, a URL is injected into the servers object.

openApiDocument.servers.unshift({
description: "Counterfact",
url,
});

That URL should include the prefix, which is stored as config.routePrefix. If we add a routePrefix argument to the openApiMiddlware() function and pass in config.routePrefix from create-koa-app.ts, we can append the prefix to the url in the above code snippet.

Note that the type of the Config object is missing routePrefix, a mistake that needs to be corrected.

@pmcelhaney pmcelhaney added the bug Something isn't working label Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants