Skip to content

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

Open
@mmihalev

Description

@mmihalev
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions