FasterXML / jackson-module-jsonSchema

Module for generating JSON Schema (v3) definitions from POJOs
371 stars 135 forks source link

Multiple occurrences of the same object results in $ref instead of inlining #102

Closed tsz662 closed 8 years ago

tsz662 commented 8 years ago

Sorry in advance if this is a duplicate of an existing issue or by design. I am using v2.7.3, but for example, with sample code like so:

public User createUser() {
    User me = new User();
    me.age = 30;
    me.firstName = new Name("foo");
    me.lastName = new Name("bar");
}

class User {
    public int age;
    public Name firstName;
    public Name lastName;
}

class Name {
    private final String name;

    Name(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }
}

I get the following output.

{
  "type" : "object",
  "id" : "urn:jsonschema:com:tsz662:sample:User",
  "properties" : {
    "age" : {
      "type" : "integer"
    },
    "firstName" : {
      "type" : "object",
      "id" : "urn:jsonschema:com:tsz662:sample:Name",
      "properties" : {
        "name" : {
          "type" : "string"
        }
      }
    },
    "lastName" : {
      "type" : "object",
      "$ref" : "urn:jsonschema:com:tsz662:sample:Name"
    }
  }
}

The lastName property is $refed, not inlined, possibly because firstName has already been inlined.
Is this by design or should I override VisitorContext?

tsz662 commented 8 years ago

Never mind. I will just override VisitorContext.getSeenSchemaUri() to return null.

tsz662 commented 8 years ago

I have to add that the above works as long as the model objects do not self-reference themselves.