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] Nullpointer Exception generating Jaxrs #6511

Closed
maha79 opened this issue Sep 18, 2017 · 6 comments
Closed

[JAVA] Nullpointer Exception generating Jaxrs #6511

maha79 opened this issue Sep 18, 2017 · 6 comments

Comments

@maha79
Copy link

maha79 commented Sep 18, 2017

Description

When generating JaxRS code, I run in a Nullpointer exception.
This is caused by a string array within the body parameter in the json description.
The problem is in AbstractJavaCodegen Method

protected boolean needToImport(String type) {
    return super.needToImport(type) && type.indexOf(".") < 0;
}

There is a null value in the list of imports to check. This in turn results from this code in DefaultCodegen.java (Line 2581):

    // recursively add import
    CodegenProperty innerCp = cp;
    while(innerCp != null) {
       imports.add(innerCp.complexType);
       innerCp = innerCp.items;
    }

innerCp is not a complexType is this case what causes NULL to be added to the import.

Swagger-codegen version

2.2.3

Swagger declaration file content or url
"paths": {		
	    "/test": {
            "post": {
                "summary": "Test method to show error",
                "operationId": "test",
                "parameters": [
					{
						"name": "queryparam1",
						"in": "query",
						"type": "string"
					}, 
                    {
                        "name": "listOfStrings",
                        "in": "body",
                        "description": "An array of strings",
                        "required": true,
                        "schema": {
		                    "type": "array",
		                    "items": {
		                        "type": "string"
		                    }
                        }
                    }
                ]
            }
        }     
Command line used for generation
    <plugin>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-codegen-maven-plugin</artifactId>
        <version>2.2.3</version>            
        <executions>                                    
  			<execution>
  				<id>generation-java</id>
  				<goals>
  					<goal>generate</goal>
  				</goals>
  				<configuration>
  					<inputSpec>test.json</inputSpec>
  					<language>jaxrs</language>  <!-- Defaults to jersey2 -->
  				</configuration>
  			</execution>
        </executions>
     </plugin>
Suggest a fix/enhancement

Add a null check to the needToImport method.

@wing328
Copy link
Contributor

wing328 commented Sep 19, 2017

@maha79 thanks for reporting the issue. Do you mind submitting a PR with the suggested fix?

@set321go
Copy link

I'm seeing the same issue, I tracked down these PR's from the release notes that seem to concern the importMappings (which i think is causing the problem).
#5384
#6063

I tried adding importMappings but that didn't change anything.

task swaggerGen {
    doLast {
        def config = new CodegenConfigurator()
        config.lang = "java"
        config.inputSpec = "${project(':address-rest').swaggerSpecFile}"
        config.outputDir = "${buildDir}/swagger/client"
        config.groupId = "${project.group}"
        config.artifactId = "address-java-client"
        config.artifactVersion = "${project.version}"
        config.apiPackage = "${project.group}.client.${project.parent.name}.api"
        config.modelPackage = "${project.group}.client.${project.parent.name}.model"
        config.invokerPackage = "${project.group}.client.${project.parent.name}.invoker"
        config.library = "okhttp-gson"
        config.templateDir = "${projectDir}/src/main/swagger-templates/${config.library}"

        new DefaultGenerator()
                .opts(config.toClientOptInput())
                .generate()
    }
}

@set321go
Copy link

After further investigation the line in my code that results in a null getting into the imports set is public Response getAddresses(@ApiParam(value = "IDs to get by", required = true) List<String> addressIds)

As maha79 identified this occurs at L2582

                CodegenProperty innerCp = cp;
                while(innerCp != null) {
                    imports.add(innerCp.complexType);
                    innerCp = innerCp.items;
                }

The toString for this instance is inner(List<String>) and complexType is indeed null, should it be for this type?

Looking a little further, innerCp.items contains an entry whose toString is inner(String) which also has a complexType of null.

This is new code and was added as part of #5891 I reverted this and validated that my build passes afterwards.

@wing328
Copy link
Contributor

wing328 commented Oct 26, 2017

@set321go do you mind submitting a PR with a proper null check when you've time? or share a spec so that we can more easily reproduce the issue while working on a fix later.

@wing328 wing328 modified the milestones: v2.3.0, v2.4.0 Dec 15, 2017
@akhalikov
Copy link

This should have been fixed in #6079.

@wing328
Copy link
Contributor

wing328 commented Apr 17, 2018

@akhalikov thanks for confirming the issue has been addressed.

@wing328 wing328 closed this as completed Apr 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants