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.lang.NullPointerException in ObjectArraySerializer.acceptJsonFormatVisitor() for array value with @JsonValue #1793

Closed
vdemay opened this issue Oct 12, 2017 · 1 comment
Milestone

Comments

@vdemay
Copy link

vdemay commented Oct 12, 2017

I'm using jackson 2.9.1

I'm trying to get JSON schema as following :


import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;

public class SchemaGenerationTest {

    public static class Item {
        public String test;
    }

    @JsonRootName("root")
    public static class RootName {
        public Item[] items;

        @JsonCreator
        public RootName(Item[] queries) {
            this.items = items;
        }

        @JsonValue
        public Item[] getItems() {
            return items;
        }
    }

    /**
     * This throws an NPE :
     *
     * java.lang.NullPointerException
     *   at com.fasterxml.jackson.databind.ser.std.ObjectArraySerializer.acceptJsonFormatVisitor(ObjectArraySerializer.java:316)
     *   at com.fasterxml.jackson.databind.ser.std.JsonValueSerializer.acceptJsonFormatVisitor(JsonValueSerializer.java:265)
     *   at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.acceptJsonFormatVisitor(DefaultSerializerProvider.java:566)
     *   at com.fasterxml.jackson.databind.ObjectMapper.acceptJsonFormatVisitor(ObjectMapper.java:3862)
     */
    @Test
    public void testSchema() throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        JavaType type = mapper.constructType(RootName.class);

        SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
        mapper.acceptJsonFormatVisitor(type, visitor);
        JsonSchema jsonSchema = visitor.finalSchema();
        try {
            System.out.println(mapper
                    .writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}

But when I'm running the test I get this exception:

java.lang.NullPointerException
	at com.fasterxml.jackson.databind.ser.std.ObjectArraySerializer.acceptJsonFormatVisitor(ObjectArraySerializer.java:316)
	at com.fasterxml.jackson.databind.ser.std.JsonValueSerializer.acceptJsonFormatVisitor(JsonValueSerializer.java:265)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.acceptJsonFormatVisitor(DefaultSerializerProvider.java:566)
	at com.fasterxml.jackson.databind.ObjectMapper.acceptJsonFormatVisitor(ObjectMapper.java:3862)

I'm suspecting JsonValueSerializer#acceptJsonFormatVisitor.
It returns ser.acceptJsonFormatVisitor(visitor, null); leading to the NPE in ObjectArraySerializer

@cowtowncoder
Copy link
Member

Thank you for reporting this. I'll see where the issue lies: some of null checks are required, others shouldn't be...

@cowtowncoder cowtowncoder added this to the 2.9.2 milestone Oct 13, 2017
@cowtowncoder cowtowncoder changed the title java.lang.NullPointerException in ObjectArraySerializer#acceptJsonFormatVisitor java.lang.NullPointerException in ObjectArraySerializer.acceptJsonFormatVisitor() for array value with @JsonValue Oct 13, 2017
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

2 participants