OpenAPITools / OpenAPITools/openapi-generator

[BUG] From + Json endpoint issue

Open
#4,625 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Hi,

Description

In a yaml api file I'm using, I got something like that :

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

Pretty nice and all, the code generator will create a function that look like :
public okhttp3.Call loginCall(String username, String password, final ApiCallback _callback) throws ApiException {

Still nice, the code will then put this in the localVarFormParams

        if (username != null) {
            localVarFormParams.put("username", username);
        }

        if (password != null) {
            localVarFormParams.put("password", password);
        }

Great ! But then, in the ApiClient.buildRequest(), everything whent wrong.
The only moment where formParams are used are here :

        if (!HttpMethod.permitsRequestBody(method)) {
            reqBody = null;
        } else if ("application/x-www-form-urlencoded".equals(contentType)) {
            reqBody = buildRequestBodyFormEncoding(formParams);
        } else if ("multipart/form-data".equals(contentType)) {
            reqBody = buildRequestBodyMultipart(formParams);
        }

Sadly, as you can see, as beside "application/x-www-form-urlencoded" I got the "application/json" content type, the ApiClient.selectHeaderContentType will always select it. Therefore, my parameters will never be used :'(

If I remove the application/x-www-form-urlencoded: part, it'll generate a loginCall with an inlineObject and everything work nice.

openapi-generator version

org.openapitools
openapi-generator-maven-plugin
4.2.1

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  description: >-
    none
    none
  version: '1.25.1'
  title: none
  contact:
    email: none
  license:
    name: none
tags:
  - name: authentication
    description: Authentication operations
paths:
  '/_login':
    post:
      tags:
        - authentication
      summary: Authenticate before using all services using form data
      description: public endpoint
      operationId: login
      requestBody:
        required: true
        content:
          application/x-www-form-urlencoded:
            schema:
              type: object
              required:
                - username
                - password
              properties:
                username:
                  type: string
                password:
                  type: string
          application/json:
            schema:
              type: object
              required:
                - username
                - password
              properties:
                username:
                  type: string
                password:
                  type: string
      responses:
        '200':
          description: Login successful or login failure
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                  token:
                    type: string
                    description: >
                      The token to send in each subsequent call on protected
                      endpoints. Empty if `success` is false.
        '500':
          description: Request malformed
          content:
            text/plain:
              schema:
                type: string
Command line used for generation

It run in a pretty clean project with maven through

<plugin>
				<groupId>org.openapitools</groupId>
				<artifactId>openapi-generator-maven-plugin</artifactId>
				<version>4.2.1</version>
				<executions>
					<execution>
						<id>api</id>
						<goals>
							<goal>generate</goal>
						</goals>
						<configuration>
							<inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
							<output>${project.build.directory}/generated-sources/api/</output>
							<modelPackage>tech.armis.api.model</modelPackage>
							<apiPackage>tech.armis.api.services</apiPackage>
							<generatorName>java</generatorName>
							<configOptions>
								<useGzipFeature>false</useGzipFeature>
								<useRuntimeException>true</useRuntimeException>
								<sourceFolder>src/gen/java/main/api</sourceFolder>
							</configOptions>
						</configuration>
					</execution>
				</executions>
			</plugin>
Steps to reproduce

Having both content type I think should do the trick

Related issues/PRs

Note sure :/

Suggest a fix

I guess, it should generate both loginCall with parameter and object, and use the right contenttype and not always json one. I guess. Maybe this yaml file is just totally wrong tho.

Thanks

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

Start with the Java generator's generated ApiClient.buildRequest() and selectHeaderContentType() paths, then reproduce the supplied OpenAPI definition with version 4.2.1. Trace how application/x-www-form-urlencoded and application/json request bodies map to generated parameters and verify that the completed behavior uses the selected content type and sends the corresponding values.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.