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

NullPointerException for Enum properties starting with Jackson 2.11 #3858

Open
lukasriedler opened this issue Jan 20, 2021 · 2 comments
Open

Comments

@lukasriedler
Copy link

lukasriedler commented Jan 20, 2021

If you have a class that looks like this (unrealistic):

public class EnumDto {

    private Enum<?> enumProperty;

    public Enum<?> getEnumProperty() {
        return enumProperty;
    }

    public void setEnumProperty(Enum<?> enumProperty) {
        this.enumProperty = enumProperty;
    }
}

or like this (stripped down version of our class; which has extending classes with the actual enum implementations used for serialization):

public abstract class EnumDto<O extends Enum<O>> {

    private O enumProperty;

    public O getEnumProperty() {
        return enumProperty;
    }

    public void setEnumProperty(O enumProperty) {
        this.enumProperty = enumProperty;
    }
}

then swagger fails with:

Caused by: java.lang.Exception: Error resolving API specification: null
        at io.swagger.v3.jaxrs2.integration.SwaggerLoader.resolve(SwaggerLoader.java:270)
        ... 102 more
Caused by: java.lang.NullPointerException
        at io.swagger.v3.core.jackson.ModelResolver._addEnumProps(ModelResolver.java:944)
        at io.swagger.v3.core.jackson.ModelResolver.resolve(ModelResolver.java:285)
        at io.swagger.v3.core.converter.ModelConverterContextImpl.resolve(ModelConverterContextImpl.java:97)
        at io.swagger.v3.core.jackson.ModelResolver.resolve(ModelResolver.java:654)
        at io.swagger.v3.core.converter.ModelConverterContextImpl.resolve(ModelConverterContextImpl.java:97)
        at io.swagger.v3.core.converter.ModelConverters.resolveAsResolvedSchema(ModelConverters.java:117)
        at io.swagger.v3.jaxrs2.Reader.parseMethod(Reader.java:1065)
        at io.swagger.v3.jaxrs2.Reader.parseMethod(Reader.java:863)
        at io.swagger.v3.jaxrs2.Reader.read(Reader.java:459)
        at io.swagger.v3.jaxrs2.Reader.read(Reader.java:168)
        at io.swagger.v3.jaxrs2.Reader.read(Reader.java:196)
        at io.swagger.v3.oas.integration.GenericOpenApiContext.read(GenericOpenApiContext.java:555)
        at io.swagger.v3.jaxrs2.integration.SwaggerLoader.resolve(SwaggerLoader.java:235)
        ... 102 more

Looks like this is because of Jackson #2480
Where they separated out a new JavaType.isEnumImplType method starting with Jackson 2.11.
Now java.lang.Enum is detected as isEnumType, but because it has no enum constants it runs into this NPE

Tested with swagger 2.1.6

@russm3086
Copy link

What is the workaround or fix for this issue?

@MonsieurPapillon
Copy link

@russm3086
One workaround you could have for now, if you are building with maven, is to precise your dependencies for jackson artifacts. Basically, use versions prior to 2.11 (I've made it work with 2.9).

I've identified 3 that work together and need to have compatible versions : jackson-databind (which is here the main problem in this issue), jackson-core and jackson-annotations. There may be more depending what kind of libraries you are using.

By precising the version on your pom.xml, you will "override" the ones brought with swagger-core, even if they are more recent.

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

3 participants