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

SchemaGenerator generates additional properties wrongly. #143

Open
rohnigam opened this issue Sep 15, 2021 · 4 comments
Open

SchemaGenerator generates additional properties wrongly. #143

rohnigam opened this issue Sep 15, 2021 · 4 comments

Comments

@rohnigam
Copy link

rohnigam commented Sep 15, 2021

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
Copy link
Author

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.

@cowtowncoder
Copy link
Member

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.

@rohnigam
Copy link
Author

@cowtowncoder This works. Thanks 👍

@cowtowncoder
Copy link
Member

Glad it works @rohnigam!

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