swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.02k stars 6.03k forks source link

swagger-codegen-maven plugin 3.1.7 -SpringMvcApiReader parseMethod Nullpointer #8864

Open sinjap opened 6 years ago

sinjap commented 6 years ago
[ERROR] Failed to execute goal com.github.kongchen:swagger-maven-plugin:3.1.7:generate (default) on project disp-web: null: MojoExecutionException: NullPointerException -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal com.github.kongchen:swagger-maven-plugin:3.1.7:generate (default) on project disp-web: null
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException
    at com.github.kongchen.swagger.docgen.mavenplugin.ApiDocumentMojo.execute(ApiDocumentMojo.java:133)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
    ... 20 more
Caused by: java.lang.NullPointerException
    at com.github.kongchen.swagger.docgen.reader.SpringMvcApiReader.parseMethod(SpringMvcApiReader.java:324)
    at com.github.kongchen.swagger.docgen.reader.SpringMvcApiReader.read(SpringMvcApiReader.java:131)
    at com.github.kongchen.swagger.docgen.reader.SpringMvcApiReader.read(SpringMvcApiReader.java:74)
    at com.github.kongchen.swagger.docgen.AbstractDocumentSource.loadDocuments(AbstractDocumentSource.java:104)
    at com.github.kongchen.swagger.docgen.mavenplugin.ApiDocumentMojo.execute(ApiDocumentMojo.java:96)
    ... 22 more

This happens when I add a @Pathvariable in my method. My method signature is as following:

@ApiOperation(value = "findAllTemplateThirdParty", notes = "Find all TemplateThirdParty Resources", response = TemplateThirdPartyResponse.class, tags = { "Template Resource" })
@ApiResponses(value = { 
    @ApiResponse(code = 200, message = "OK", response = TemplateThirdPartyResponse.class),
    @ApiResponse(code = 400, message = "BAD REQUEST", response = ApiResponseMessage.class),
    @ApiResponse(code = 401, message = "UNAUTHORIZED", response = Void.class),
    @ApiResponse(code = 404, message = "NOT FOUND", response = Void.class) })
@RequestMapping(value = "/admin/templates/{templateId}/thirdParties", 
    produces = { "application/json", "application/hal+json", "text/uri-list", "application/x-spring-data-compact+json" },
    consumes = { "application/json" }, method = RequestMethod.GET)
ResponseEntity<PagedResources<TemplateThirdPartyResponse>> findAllTemplateThirdPartyUsingGET(
    @ApiParam(value = "templateId", required = true) @PathVariable Integer templateId,
    @QuerydslPredicate(root = entities.TemplateThirdParty.class) Predicate predicate,
    @PageableDefault(sort = { "id" }) Pageable pageable,
    @RequestParam MultiValueMap<String, String> parameters);

Without the @Pathvariable, the swagger-codegen runs fine but not acceptable for me. Please could you investigate the underlying issue?

grubeninspekteur commented 2 years ago

Stumbled upon this, the underlying issue is that the parameter name is not available via reflection. Either add the "-parameters" compiler option or specify the name in the @PathVariable directly.

Would be interested to know though how Spring does it at runtime when parameter names are not compiled.