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.38k stars 8.92k forks source link

URL Fragment should be removed from path when building the request #6620

Open cbornet opened 3 years ago

cbornet commented 3 years ago

Q&A (please complete the following information)

Content & configuration

Example Swagger/OpenAPI definition:

---
openapi: 3.0.2
info:
  title: Swagger Petstore - OpenAPI 3.0
  version: 1.0.5
servers:
- url: "/api/v3"
paths:
  "/pet/findByStatus#test":
    get:
      tags:
      - pet
      summary: Finds Pets by status
      description: Multiple status values can be provided with comma separated strings
      operationId: findPetsByStatus
      parameters:
      - name: status
        in: query
        description: Status values that need to be considered for filter
        required: false
        explode: true
        schema:
          type: string
          default: available
          enum:
          - available
          - pending
          - sold
      responses:
        '200':
          description: successful operation
          content:
            application/xml:
              schema:
                type: array
                items:
                  "$ref": "#/components/schemas/Pet"
            application/json:
              schema:
                type: array
                items:
                  "$ref": "#/components/schemas/Pet"
        '400':
          description: Invalid status value
components:
  schemas:
    Category:
      type: object
      properties:
        id:
          type: integer
          format: int64
          example: 1
        name:
          type: string
          example: Dogs
      xml:
        name: category
    Tag:
      type: object
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
      xml:
        name: tag
    Pet:
      required:
      - name
      - photoUrls
      type: object
      properties:
        id:
          type: integer
          format: int64
          example: 10
        name:
          type: string
          example: doggie
        category:
          "$ref": "#/components/schemas/Category"
        photoUrls:
          type: array
          xml:
            wrapped: true
          items:
            type: string
            xml:
              name: photoUrl
        tags:
          type: array
          xml:
            wrapped: true
          items:
            "$ref": "#/components/schemas/Tag"
        status:
          type: string
          description: pet status in the store
          enum:
          - available
          - pending
          - sold
      xml:
        name: pet
    ApiResponse:
      type: object
      properties:
        code:
          type: integer
          format: int32
        type:
          type: string
        message:
          type: string
      xml:
        name: "##default"
  requestBodies:
    Pet:
      description: Pet object that needs to be added to the store
      content:
        application/json:
          schema:
            "$ref": "#/components/schemas/Pet"
        application/xml:
          schema:
            "$ref": "#/components/schemas/Pet"

Describe the bug you're encountering

If a path contains a fragment, the query params are appended after the fragment. Clients (browser, curl, ...) don't send what is after the fragment so the query params are not sent. When building the URL for the request, the fragment should be searched and removed.

To reproduce...

Steps to reproduce the behavior:

  1. Use the example YAML in swagger-ui
  2. Click on /pet/findByStatus#test "Try it out" and execute
  3. A request to /pet/findByStatus is made (without the status query param)

Expected behavior

A request to /pet/findByStatus?status=available is made.

Additional context or thoughts

Using a fragment is a common workaround for the impossibility to have multiple operations on the same path+method.

hkosova commented 3 years ago

Paths in OpenAPI definitions are not supposed to include fragments at all. See https://github.com/OAI/OpenAPI-Specification/issues/1635, specifically this comment that references RFC 8820 section 2.5:

other Specifications MUST NOT define structure within the fragment identifier, unless they are explicitly defining one for reuse by media types in their definitions (for example, as JSON Pointer [RFC6901] does).

cbornet commented 3 years ago

I know. Yet a lot of people use this technique as a workaround. See https://github.com/OAI/OpenAPI-Specification/issues/182 for instance. It seems swagger-ui could support it at little cost.