swagger-api / swagger-api/swagger-codegen

[PHP] Generator doesn't respect the optional parameters in request body when content type is application/x-www-form-urlencoded

Open
#11,129 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

We have a request body with a schema with "application/x-www-form-urlencoded" content type. There are some parameters which are set as required. One of the parameters is optional. Swagger UI renders everything correctly and there are no issues there. However, when we generate PHP client with the swagger-codegen, we see that all the parameters are required... even the optional one. This basically makes the generated client useless.

Swagger-codegen version

3.0.26, 3.0.27

Swagger declaration file content or url

This is the definition:

post:
  tags:
    - Support
  summary: Create message
  description: Write message into a ticket
  operationId: support_create_message
  parameters:
    - name: ticket_id
      in: path
      description: Ticket ID
      required: true
      schema:
        type: string
  requestBody:
    $ref: '#/components/requestBodies/ticketsMessagesCreate'

And this is the schema of the requestBody:

ticketsMessagesCreate:
      description: ""
      required: true
      content:
        application/x-www-form-urlencoded:
          schema:
            type: object
            properties:
              message:
                description: "Ticket message."
                type: string
                minLength: 1
                maxLength: 5000
              display_name:
                description: "Name of the person who wrote the message"
                type: string
                minLength: 1
                maxLength: 100
              display_email:
                description: "Email of the person who wrote the message"
                type: string
                format: email
              attachments[]:
                description: "File attachments. Must be an array with attachment identifiers obtained from the [/support/messages/attachment](#operation/support_upload_attachment) method."
                type: array
                items:
                  type: integer
                  nullable: false
            required: 
              - message
              - display_name
              - display_email		

Full specs can be found here: https://api.tuningfiles.com/swagger.json

As you can see from the above, only message, display_name and display_email are required from the request body. attachments[] is optional. However generated php code looks like this:

    /**
     * Create request for operation 'supportCreateMessage'
     *
     * @param  string $message (required)
     * @param  string $display_name (required)
     * @param  string $display_email (required)
     * @param  int[] $attachments (required)
     * @param  string $ticket_id Ticket ID (required)
     *
     * @throws \InvalidArgumentException
     * @return \GuzzleHttp\Psr7\Request
     */
    protected function supportCreateMessageRequest($message, $display_name, $display_email, $attachments, $ticket_id)
    {
        .....
        // verify the required parameter 'attachments' is set
        if ($attachments === null || (is_array($attachments) && count($attachments) === 0)) {
            throw new \InvalidArgumentException(
                'Missing the required parameter $attachments when calling supportCreateMessage'
            );
        }
       .....

As you can see from the code above, $attachments are also required , but it shouldn't.

Command line used for generation

swagger-codegen generate -i https://api.tuningfiles.com/swagger.json -l php -o /output_dir -c config.json

config.json:

{
    "composerProjectName": "project name",
    "artifactVersion": "version",
    "sortParamsByRequiredFlag": true,
    "packagePath": "",
    "apiPackage": "Api",
    "invokerPackage": "Package name",
    "composerVendorName": "vendor name",
    "ensureUniqueParams": false,
    "variableNamingConvention": "snake_case",
    "allowUnicodeIdentifiers": false,
    "gitUserId": "git user",
    "srcBasePath": "",
    "hideGenerationTimestamp": true,
    "modelPackage": "",
    "gitRepoId": "repo id"
}
Steps to reproduce

Using the schema from https://api.tuningfiles.com/swagger.json and config.json from above, use the following command to generate php client (replace the output dir with your own directory):

  1. swagger-codegen generate -i https://api.tuningfiles.com/swagger.json -l php -o /output_dir -c config.json
  2. Navigate to the folder where client is generated and open Api/SupportApi.php
  3. Locate protected function supportCreateMessageRequest function (around line 596) and you will see that all the parameters are required. Even the $attachments parameter. No matter that it's not included into the required params in the specification.
Related issues/PRs

I can't find anything related.

Suggest a fix/enhancement

N/A

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the generation using the provided OpenAPI schema, config.json, and the swagger-codegen command, then inspect the generated Api/SupportApi.php and supportCreateMessageRequest entry point. Compare the request body's required list with the generated parameter validation, including attachments[]. Done when the optional form parameter is no longer treated as required in the generated PHP client.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.