swagger-api / swagger-ui

Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
https://swagger.io
Apache License 2.0
26.48k stars 8.96k forks source link

Swagger UI within docker and one (or multiple) referenced ($ref) YAML file(s) #5008

Open CyrilleBenard opened 5 years ago

CyrilleBenard commented 5 years ago

Content & configuration

Swagger/OpenAPI definition:

Not the full YAML file but an extract to identify what's the purpose of this improvement I think that would be nice

paths:
  /{ueContextId}/provide:
    post:
      summary: Namf_Location ProvideLocation service Operation
      tags:
        - Provide Location
      operationId: ProvideLocation
      parameters:
        - name: ueContextId
          in: path
          description: UE Context Identifier
          required: true
          schema:
            type: string
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/RequestUELocation'
        required: true
      responses:
        '200':
          description: Expected response to a valid request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ProvideUELocation'
        '403':
          $ref: './TS29571_CommonData.yaml#/components/responses/403'

Is your feature request related to a problem?

The current docker image already downloadable allows the user to specify its own JSON / YAML file as soon as the container ran, thanks to the below CLI :

docker run -p 80:8080 -e SWAGGER_JSON=/foo/openapi.yaml -v /bar:/foo swaggerapi/swagger-ui

But in case the current YAML file (openapi.yaml) uses an external file reference (see _$ref: './TS29571CommonData.yaml#/components/responses/403' above), Swagger-UI will not be able to handle it.

Describe the solution you'd like

In the docker-run.sh file I would have replaced the link to the JSON file by a link on each file in its current directory :

if [[ -f $SWAGGER_JSON ]]; then
# OLD CODE 
  # cp -s $SWAGGER_JSON $NGINX_ROOT
# NEW CODE 
  cd $NGINX_ROOT ; ln -s $(dirname $SWAGGER_JSON)/* .

# The others lines stay as they are
  REL_PATH="./$(basename $SWAGGER_JSON)"
  sed -i "s|https://petstore.swagger.io/v2/swagger.json|$REL_PATH|g" $INDEX_FILE
  sed -i "s|http://example.com/api|$REL_PATH|g" $INDEX_FILE
else
  sed -i "s|https://petstore.swagger.io/v2/swagger.json|$API_URL|g" $INDEX_FILE
  sed -i "s|http://example.com/api|$API_URL|g" $INDEX_FILE
fi
hottehead commented 5 years ago

I have forked the project and implemented this. See my fork and the respecting docker image at docker hub

I still have a problem with references within component defintions. If such a reference is present the browser will load the referenced file over and over again.