swag icon indicating copy to clipboard operation
swag copied to clipboard

Request body param always generate `oneOf`, and incorrectly (v2)

Open vnen opened this issue 5 months ago • 1 comments

Describe the bug The @Param tag with body and an object type always generate an oneOf list even if it's alone and the entries are also created incorrectly.

Example output:

  /user:
    put:
      description: Update user.
      requestBody:
        content:
          application/json:
            schema:
              oneOf:
              - type: object
              - $ref: '#/components/schemas/dto.UpdateUserInput'
                description: Update User Data
                summary: data
        description: Update User Data
        required: true
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/dto.UpdateUserOutput'
          description: OK

Note how the request schema contains a oneOf with 2 items:

schema:
  oneOf:
  - type: object
  - $ref: '#/components/schemas/dto.UpdateUserInput'
    description: Update User Data
    summary: data

The first item is type: object and the second is the $ref. Instead, it should generate a single object:

schema:
  oneOf:
  - type: object
    $ref: '#/components/schemas/dto.UpdateUserInput'
    description: Update User Data
    summary: data

Note the missing - (dash) before $ref. This makes the type part of the schema. Or remove the type altogether since the reference implies it.

Of course, ideally in this case there should not be a oneOf. My guess this happens because there are 2 spurious entries (one with type and one with the reference) instead of a single one.

To Reproduce Using the following comment:

//	@Summary		Update user.
//
//	@Description	Update user.
//
//	@Accept			json
//	@Produce		json
//
//	@Param			data body dto.UpdateUserInput true "Update User Data"
//
//	@Success		200	{object}	dto.UpdateUserOutput
//
//	@Router			/user [put]

Your swag version v2.0.0 (bc4b08e2cb1f08b91cf71aba863546978964cc64)

Your go version 1.25.3

Desktop (please complete the following information):

  • OS: Arch Linux
  • Browser: N/A
  • Version: N/A

vnen avatar Oct 27 '25 23:10 vnen