jackson-module-jsonSchema icon indicating copy to clipboard operation
jackson-module-jsonSchema copied to clipboard

SchemaGenerator generates additional properties wrongly.

Open rohnigam opened this issue 4 years ago • 4 comments

I have a class like below:

public class MyClass{
    @JsonProperty("property_a")
    private A a;

    @JsonProperty("property_b")
    @JsonPropertyDescription("my favourite property")
    private B b;

    @JsonProperty("property_c")
    private C c;

    /**
    * propagate some internals of A
    */
    public List<String> getSomethingAboutA() {
        if (this.a != null) {
            return this.a.getSomething();
        }
        return null;
    }
}

This will go on to generate the following json schema

 {
            "type": "object",
            "id": "urn:jsonschema:pathToMyClass.MyClass",
            "properties": {
                "property_a": {
                    "type": "object",
                },
                "property_b": {
                    "type": "object",
                    "description": "my favourite property",
                },
                "property_c": {
                    "type": "object",
                },
                "somethingAboutA": {
                    "type": "array",
                    "items": {
                        "type": "string"
                    }
                }
     }

For brevity I have excluded the details of A, B and C in both java classes and json schema generated.

Bug:

somethingAboutA is not actually a property and should not be generated.

Is there any workaround for this ? Or maybe I am missing some other configuration ?

My observation is that this is only happening when a method starts with get,
so may be, it is being interpreted as an attribute.

I am using the latest version of this library.

<dependency>
      <groupId>com.fasterxml.jackson.module</groupId>
      <artifactId>jackson-module-jsonSchema</artifactId>
      <version>2.13.0-rc2</version>
</dependency>

This is the schema generation code:

        ObjectMapper jacksonObjectMapper = new ObjectMapper();
        JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(jacksonObjectMapper);
        String schemaString = "test";
        try {
            JsonSchema schema = schemaGen.generateSchema(MyClass.class);
            schemaString = jacksonObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);
        } catch (JsonMappingException ex) {
            System.out.println(ex.toString());
        } catch (JsonProcessingException ex) {
            System.out.println(ex.toString());
        }
        System.out.println(schemaString);

rohnigam avatar Sep 15 '21 16:09 rohnigam

Found one workaround for this. Both JsonIgnore on the method getSomethingAboutA and JsonIgnoreProperties on the class like @JsonIgnoreProperties({"somethingAboutA"}) work.

But, I still feel these are workarounds, and somethingAboutA should not have been picked in the first place.

rohnigam avatar Sep 17 '21 05:09 rohnigam

This seems to work as expected: getSomethingAboutA() is a valid getter and Jackson does consider there to be a property. This is by design; by default public getters/setters with Bean-compatible name and signature are auto-detected. Similarly public fields are detected.

You can, however, change configurations so that only explicitly annotated accessors are considered. For classes that is via @JsonAutoDetect, although there is ObjectMapper configuration setting for changing baseline as well. What you want basically is to set detection level for getters (and probably setters, fields too) to Visibility.NONE, in which case only explicitly annotated properties are detected.

cowtowncoder avatar Sep 17 '21 17:09 cowtowncoder

@cowtowncoder This works. Thanks 👍

rohnigam avatar Sep 27 '21 14:09 rohnigam

Glad it works @rohnigam!

cowtowncoder avatar Sep 28 '21 01:09 cowtowncoder

For me jsonschemagenerator is not creating any instance. Giving me some exceptions. I am using Jackson 2.14.2 version

Can anyone help?

rishabhvarshney2 avatar Aug 06 '24 03:08 rishabhvarshney2

It Says CLASSNOTFOUND Exception

rishabhvarshney2 avatar Aug 09 '24 04:08 rishabhvarshney2

@rishabhvarshney2 Do not tag on unrelated things into existing issues. Instead file a new issue with enough details to explain the issue you see -- preferably using a recent version.

cowtowncoder avatar Aug 11 '24 16:08 cowtowncoder