swagger-api / swagger-api/swagger-codegen

Typing request objects in JAXRS api definition

Open
#8,402 3 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

Hi, we are using the "swagger-codegen-maven-plugin" in version 3.0.0-SNAPSHOT and we noticed that when the plugin create API definitions class doesn't typing correctly the request object.

Swagger-codegen version

3.0.0-SNAPSHOT

Command line used for generation

mvn clean install

following the configuration of the maven plugin

<plugin>
	<groupId>io.swagger</groupId>
	<artifactId>swagger-codegen-maven-plugin</artifactId>
	<version>3.0.0-SNAPSHOT</version>
	<executions>
		<execution>
			<goals>
				<goal>generate</goal>
			</goals>
			<configuration>
				<inputSpec>src/main/resources/test.json</inputSpec>
				<language>jaxrs-cxf-client</language>
				<withXml>true</withXml>
				<sourceFolder>src/main/java</sourceFolder>
				<apiPackage>codegen.api</apiPackage>
				<modelPackage>codegen.model</modelPackage>
				<generateApis>true</generateApis>
				<generateApiTest>false</generateApiTest>
				<generateApiDocumentation>false</generateApiDocumentation>
				<generateModels>true</generateModels>
				<generateModelTests>false</generateModelTests>
				<generateModelDocumentation>false</generateModelDocumentation>
				<dateLibrary>java8-localdatetime</dateLibrary>
			</configuration>
		</execution>
	</executions>
</plugin>
Swagger declaration file content or url

following the configuration of the example json

{
    "swagger": "2.0",
    "info": {
        "description": "description",
        "version": "1.0",
        "title": "Test json"
    },
    "host": "localhost:8080",
    "basePath": "/tets/servlet/services",
    "schemes": [
        "http"
    ],
    "paths": {
        "/testRequest": {
            "post": {
                "operationId": "testRequest",
                "tags": [
                    "test operation"
                ],
                "description": "",
                "consumes": [
                    "application/json"
                ],
                "produces": [
                    "application/json",
                    "text/plain"
                ],
                "parameters": [
                    {
                        "in": "body",
                        "name": "Request Body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/TestRequest"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/TestResponse"
                        }
                    }
                }
            }
        }
    },
    "definitions": {
        "TestRequest": {
            "description": "Request Message",
            "type": "object",
            "allOf": [ {
                    "properties": {
                        "body": {
                            "$ref": "#/definitions/TestRequestBody"
                        }
                    },
                    "required": [
                        "body"
                    ]
                }
            ]
        },
        "TestRequestBody": {
            "type": "object",
            "properties": {
                "TestCode": {
                    "$ref": "#/definitions/tTestCode"
                }
            },
            "required": [
                "TestCode"
            ]
        },
        "TestResponse": {
            "description": "Response Message",
            "type": "object",
            "allOf": [ {
                    "properties": {
                        "body": {
                            "$ref": "#/definitions/TestResponseBody"
                        }
                    },
                    "required": [
                        "body"
                    ]
                }
            ]
        },
        "TestResponseBody": {
            "type": "object",
            "properties": {
                "TestType": {
                    "$ref": "#/definitions/tTestType"
                }
            },
            "required": [
                "TestType"
            ]
        },
        "tTestCode": {
            "type": "integer",
            "minLength": 3,
            "maxLength": 3,
            "format": "int64"
        },
        "tTestType": {
            "type": "string",
            "minLength": 0,
            "maxLength": 20,
            "enum": [
                "Test1",
                "Test2",
                "Test3"
            ]
        }
    }
}

The resulting code, in detail, is:

@Path("/")
@Api(value = "/", description = "")
public interface TestOperationApi  {
    @POST
    @Path("/testRequest")
    @Consumes({ "application/json" })
    @Produces({ "application/json", "text/plain" })
    @ApiOperation(value = "", tags={  })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "OK", response = TestResponse.class) })
    public TestResponse testRequest(Object body);
}

instead of:

@Path("/")
@Api(value = "/", description = "")
public interface TestOperationApi  {
    @POST
    @Path("/testRequest")
    @Consumes({ "application/json" })
    @Produces({ "application/json", "text/plain" })
    @ApiOperation(value = "", tags={  })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "OK", response = TestResponse.class) })
    public TestResponse testRequest(TestRequest body);
}

In detail, we need to have a parameter input with TestRequest class intead a generic Object class.

Thank you.

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 with the swagger-codegen-maven-plugin configuration, using src/main/resources/test.json and the jaxrs-cxf-client language. Inspect the generated TestOperationApi signature and trace how the body parameter type is selected. Done means the generated method accepts TestRequest instead of Object, while preserving the shown response and annotations.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.