Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JAVA] Array of Strings as requestBody of a POST request in OAS3 #7592

Closed
jmini opened this issue Feb 6, 2018 · 2 comments
Closed

[JAVA] Array of Strings as requestBody of a POST request in OAS3 #7592

jmini opened this issue Feb 6, 2018 · 2 comments
Assignees

Comments

@jmini
Copy link
Contributor

jmini commented Feb 6, 2018

Description

According to #6745 it is possible to define POST request having a List of String as request body parameter.

In the generated Java client, the argument is a String where a List<String> is expected.

Swagger-codegen version

3.0.0-rc0.

pom.xml configuration:

  <build>
    <plugins>
      <plugin>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-codegen-maven-plugin</artifactId>
        <version>3.0.0-rc0</version>
        <executions>
          <execution>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec><!-- ... --></inputSpec>
              <language>java</language>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-codegen-generators</artifactId>
            <version>1.0.0-rc0</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
Swagger declaration file content or url
openapi: 3.0.1
info:
  title: A dummy title
  version: 1.0.0

paths:
  /my/endpoint:
    post:
      summary: Returns a list
      operationId: getMyList
      requestBody:
        description: The list to retrieve as csv
        content:
          application/json:
            schema:
              type: array
              items:
                type: string
        required: true
      responses:
        '200':
          description: Ok
          content:
            application/json:
              schema:
                type: string
# components sections is necessary to avoid a NullPointer Exception during code generation
components:
  schemas:
    Dummy:
      type: string
Command line used for generation
mvn compile
Related issues/PRs

#6745

Suggest a fix/enhancement

The method signature for my endpoint in DefaultApi is public String getMyList(String body). It should be public String getMyList(List<String> body).

The code generated in DefaultApiTest is:

    @Test
    public void getMyListTest() throws ApiException {
        
        String body = null;
        
        String response = api.getMyList(body);

        // TODO: test validations
    }

Here the same: body should be List<String>

Swagger 2.0

As comparison, I tested everything with Swagger 2.0:

swagger: "2.0"

info:
  title: A dummy title
  version: 1.0.0

paths:
  /my/endpoint:
    post:
      description: Returns a list
      operationId: getMyList
      parameters:
        - in: body
          name: body
          description: The list to retrieve as csv
          required: true
          schema:
            type: array
            items:
              type: string
      produces:
        - application/json
      responses:
        '200':
          description: Ok
          schema: {
            type: string
          }

Inspired by this StackOverflow question: Specify an array of strings as body parameter in swagger API

With io.swagger:swagger-codegen-maven-plugin:2.3.1 the generated code works with a List, as expected.

@jmini
Copy link
Contributor Author

jmini commented Feb 15, 2018

I have proposed a Pull Request in the corresponding git repository: swagger-api/swagger-codegen-generators#13

@jmini
Copy link
Contributor Author

jmini commented Feb 16, 2018

Verified with io.swagger:swagger-codegen-generators:1.0.0-SNAPSHOT. It works for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants