openapi-ext icon indicating copy to clipboard operation
openapi-ext copied to clipboard

Wrong redirection in containerized environment

Open bfedoronchuk opened this issue 4 years ago • 4 comments
trafficstars

Environment Details

  • OS: MacOS
  • Docker version: 20.10.5 Community
  • JDK version: 11
  • MicroProfile implementation: Helidon MP
  • Helidon Version: 2.3.2

Problem Description

Path /openapi-ui redirects to the wrong URI if the MicroProfile app is running inside of Docker container. Extension swaps the host and port to values defined by the container.

I haven't dived deep into the bug, but at least I realized that injected javax.ws.rs.core.UriInfo provides the wrong base URI. As far as I know, Helidon uses Jersey as JAX-RS implementation. So, this problem could be Jersey-specific.

Steps to reproduce

Steps to reproduce:

  1. Setup Helidon MP (Microprofile implementation) example project.
mvn -U archetype:generate -DinteractiveMode=false \
    -DarchetypeGroupId=io.helidon.archetypes \
    -DarchetypeArtifactId=helidon-quickstart-mp \
    -DarchetypeVersion=2.3.2 \
    -DgroupId=io.helidon.examples \
    -DartifactId=openapi-ui-issue \
    -Dpackage=io.helidon.examples.quickstart.mp;
  1. Go to pom.xml and add the dependency.
<dependency>
    <groupId>org.microprofile-ext.openapi-ext</groupId>
    <artifactId>openapi-ui</artifactId>
    <version>1.1.4</version>
</dependency>
  1. Go to the project.
cd openapi-ui-issue;
  1. Build Docker image.
docker build -t openapi-ui-issue . 
  1. Run Docker container.
docker run --rm -p 9090:8080 openapi-ui-issue:latest
  1. Open http://127.0.0.1:9090/openapi-ui in the browser. Browser redirects to the wrong path: http://172.17.0.2:8080/openapi-ui/index.html Where 172.17.0.2 is the Docker container internal IP and 8080 is the port of the app running inside of the container.

By the way, everything works fine if you specify the full path: http://127.0.0.1:9090/openapi-ui/index.html

bfedoronchuk avatar Aug 02 '21 09:08 bfedoronchuk

Hi. Thanks for this. I'll be honest I have no idea how to fix this.

But this is the problem code: https://github.com/microprofile-extensions/openapi-ext/blob/dac59beac4c765fc5aff60d658e90986793a4698/openapi-ui/src/main/java/org/microprofileext/openapi/swaggerui/OpenApiUiService.java#L66

We can maybe introduce a new config where the user can specify the correct base in this case. Or, we can let the redirect happen on the client, so download a small html on this url that does the redirect.

You are welcome to do a PR ??

phillip-kruger avatar Aug 03 '21 18:08 phillip-kruger

Sure, will try.

bfedoronchuk avatar Aug 13 '21 22:08 bfedoronchuk

I currently stumbled over this issue. I thought this feature is already implemented? There is this environment variable MP_OPENAPI_SERVERS which you can set for example in a docker-compose file:

  my-service:
    image: imixs/my-service
    environment:
      # Payara-Micor ENV
      MP_OPENAPI_SERVERS: "http://localhost:8081"

and I have a project in production where I used this and it works.

But now I currently try to build another project and the variable is ignored. So now I have the same problem and I wonder how I have managed to get this working in my other project (based on microprofile 2.2)

See also the openapi documentation here

rsoika avatar Jan 12 '22 21:01 rsoika

I figured out that it is an issue from openapi and not from the openapi-ui. If you annotate your rest application with the correct data the information is shown through openapi and also used by openapi-ui

@ApplicationPath("api")
@OpenAPIDefinition(info = @Info(
        title = "Example application", 
        version = "1.0.0", 
        contact = @Contact(
                name = "John", 
                email = "[email protected]",
                url = "https://www.foo.com")
        ),
        servers = {
            @Server(url = "/",description = "localhost")
        }
)
public class BaseApplication extends Application {

}

Now I wonder how I can overwrite the 'severs' sections in a container based runtime environment....? The environment variable MP_OPENAPI_SERVERS id no longer work

rsoika avatar Jan 12 '22 21:01 rsoika