kongchen / swagger-maven-plugin

JAX-RS & SpringMVC supported maven build plugin, helps you generate Swagger JSON and API document in build phase.
http://kongchen.github.io/swagger-maven-plugin/
Apache License 2.0
761 stars 450 forks source link

java.lang.NullPointerException in SpringSwaggerExtension.java:219 when using @requestBody with generics #589

Open papaanoel opened 6 years ago

papaanoel commented 6 years ago

swagger-maven-plugin:3.1.6:generate maven goal throws a NullPointerException when using generics in requestBody params :

Caused by: java.lang.NullPointerException
    at com.github.kongchen.swagger.docgen.spring.SpringSwaggerExtension.shouldIgnoreType (SpringSwaggerExtension.java:219)
    at com.github.kongchen.swagger.docgen.spring.SpringSwaggerExtension.extractParameters (SpringSwaggerExtension.java:45)
    at com.github.kongchen.swagger.docgen.reader.AbstractReader.getParameters (AbstractReader.java:399)
    at com.github.kongchen.swagger.docgen.reader.SpringMvcApiReader.parseMethod (SpringMvcApiReader.java:314)
    at com.github.kongchen.swagger.docgen.reader.SpringMvcApiReader.read (SpringMvcApiReader.java:127)
    at com.github.kongchen.swagger.docgen.reader.SpringMvcApiReader.read (SpringMvcApiReader.java:70)

Here is the pattern i'm trying to achieve :

public abstract class AbstractRestFacade<T extends Entity, K extends EntityDTO> {

  @Autowired
  protected NameValueService<T, K> service;

  @PutMapping
  @ApiOperation(value = "Modification")
  public K update(@RequestBody K entityDTO) throws FonctionnelleException {
    return service.update(entityDTO, super.getConnectedUser());
  }
}

and

@RestController
@RequestMapping(LotSecuredRestFacade.ROOT_RESOURCE)
@Api("API for managing Entities")
@AllArgsConstructor
public class MyRestFacade extends AbstractRestFacade<MyEntity, MyEntityDTO> {
  public static final String ROOT_RESOURCE = "/api/secured/lots";
}

Thanks.

coderatiiita commented 2 years ago

what is the solution for this?

mpmartins commented 8 months ago

I was poking around and found a way to get past this issue. I'm sharing this in case someone wants to take this and create a proper PR:

I made the changes bellow on the com.github.kongchen.swagger.docgen.reader.SpringMvcApiReader#parseMethod method:

// process parameters
        Class[] parameterTypes = method.getParameterTypes();
        Annotation[][] paramAnnotations = method.getParameterAnnotations();
        DefaultParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
        String[] parameterNames = parameterNameDiscoverer.getParameterNames(method);
        // paramTypes = method.getParameterTypes
        // genericParamTypes = method.getGenericParameterTypes
        for (int i = 0; i < parameterTypes.length; i++) {
            Type type = parameterTypes[i];
            List<Annotation> annotations = Arrays.asList(paramAnnotations[i]);
            List<Parameter> parameters = getParameters(type, annotations);

            for (Parameter parameter : parameters) {
                if(parameter.getName().isEmpty()) {
                    parameter.setName(parameterNames[i]);
                }
                operation.parameter(parameter);
            }
        }