Siddhanta Rath
Siddhanta Rath

Reputation: 1036

How to fix validation error getting from openapi go code generator for an API which consumes application/x-www-form-urlencoded content type?

I have followed the open api guidelines mention here to define an API which consumes application/x-www-form-urlencoded and written below API:

{
    "openapi": "3.0.0",
    "info": {
        "version": "1.0.draft",
        "title": "user management api",
        "description": "This document defines interface to user management"
    },
    "servers": [{
        "url": "{apiRoot}/test",
        "variables": {
            "apiRoot": {
                "default": "https://example.com"
            }
        }
    }],
    "paths": {
        "/users/resetpassword": {
            "post": {
                "summary": "reset password of a user",
                "operationId": "resetUserPassword",
                "parameters": [{
                        "name": "username",
                        "in": "formData",
                        "required": true,
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "name": "password",
                        "in": "formData",
                        "required": true,
                        "schema": {
                            "type": "string"
                        }
                    }
                ],
                "responses": {
                    "204": {
                        "description": "User password has been changed"
                    }
                }
            }
        }
    }
}

ran below command to generate go code from above open API doc using a code generator:

docker run --rm -v ${PWD}:<file_path> openapitools/openapi-generator-cli generate -i <file_path> --skip-validate-spec -g go-server -o <file_out_path>

got below error:

-attribute paths.'/users/resetpassword'(post).parameters.[username].in is not of type `string`
-attribute paths.'/users/resetpassword'(post).parameters.[password].in is not of type `string`

How to fix above error?

Upvotes: 1

Views: 1990

Answers (1)

Helen
Helen

Reputation: 97550

You've mixed up OpenAPI 2.0 and OpenAPI 3.0 syntax.

In OpenAPI 3.0, request bodies (including form data) are defined by using the requestBody keyword. Replace the parameters section with:

"requestBody": {
  "required": true,
  "content": {
    "application/x-www-form-urlencoded": {
      "schema": {
        "type": "object",
        "required": [
          "username",
          "password"
        ],
        "properties": {
          "username": {
            "type": "string"
          },
          "password": {
            "type": "string"
          }
        }
      }
    }
  }
}

YAML version:

requestBody:
  required: true
  content:
    application/x-www-form-urlencoded:
      schema:
        type: object
        required:
        - username
        - password
        properties:
          username:
            type: string
          password:
            type: string

Upvotes: 1

Related Questions