Skip to content

java.lang.NullPointerException in ObjectArraySerializer.acceptJsonFormatVisitor() for array value with @JsonValue #1793

@vdemay

Description

@vdemay

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions