0

I know there are a lot of questions related to generating the JSONSchema from JAXB annotated classes using the Jackson but I could not find any example where JSONSchema generated using the MOXY annotated class.

All I want to know is how can I generate JSONSchema for my MOXY annotated class? As of now when I generate the JSONSchema for my JAXB/Moxy annotated class then I get only one field:

{
  "type" : "any"
}

Following is my class for which I would like to generate JSONSchema using the Jackson and JAXB/Moxy annotations: (As you can see I have name and age as mandatory field but they don't show up in the generated JSONSchema)

@XmlRootElement(name = "Customer")
@XmlType(name = "Customer", propOrder = {"name", "age", "userExtensions"})
@XmlAccessorType(XmlAccessType.FIELD)
@NoArgsConstructor
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
@AllArgsConstructor
public class Customer extends Person {

  @XmlElement(name = "name",required = true)
  private String name;

  @XmlElement(name = "name",required = true)
  private String age;

  @JsonSerialize(using = CustomExtensionsSerializer.class)
  @XmlJavaTypeAdapter(TestAdapter.class)
  @XmlPath(".")
  @JsonValue
  private Map<String, Object> userExtensions = new HashMap<>();

  @JsonAnySetter
  public void setUserExtensions(String key, Object value) {
    userExtensions.put(key, value);
  }

  public Map<String, Object> getUserExtensions() {
    return userExtensions;
  }
}

Following is my Main class which generates the JSONSchema:

class JsonSchemaGenerator{

  public static void main(String[] args) throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    TypeFactory typeFactory = TypeFactory.defaultInstance();
    AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(typeFactory);
    objectMapper.getDeserializationConfig().with(introspector);
    objectMapper.getSerializationConfig().with(introspector);

    //To force mapper to include JAXB annotated properties in Json schema
    objectMapper.registerModule(new JaxbAnnotationModule());
    SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
    objectMapper.acceptJsonFormatVisitor(objectMapper.constructType(Customer.class), visitor);

    JsonSchema inputSchema = visitor.finalSchema();
    String schemaString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(inputSchema);

    System.out.println(schemaString);
  }
}
BATMAN_2008
  • 2,788
  • 3
  • 31
  • 98
  • After trying a lot of things I found that it's due to the annotation `@JsonValue`. If I remove this then the `JSON Schema` is generated correctly. I want to know how to generate the `JSON Schema` with the `@JsonValue` annotation. – BATMAN_2008 Jun 04 '21 at 05:38

1 Answers1

0

I am using the @JsonValue with my Map<String,Object> that's the reason I was getting just type:any. I removed it and then tried and it worked for me.

@XmlRootElement(name = "Customer")
@XmlType(name = "Customer", propOrder = {"name", "age", "userExtensions"})
@XmlAccessorType(XmlAccessType.FIELD)
@NoArgsConstructor
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
@AllArgsConstructor
public class Customer extends Person {

  @XmlElement(name = "name",required = true)
  private String name;

  @XmlElement(name = "name",required = true)
  private String age;

  @JsonSerialize(using = CustomExtensionsSerializer.class)
  @XmlJavaTypeAdapter(TestAdapter.class)
  @XmlPath(".")
  private Map<String, Object> userExtensions = new HashMap<>();

  @JsonAnySetter
  public void setUserExtensions(String key, Object value) {
    userExtensions.put(key, value);
  }

  public Map<String, Object> getUserExtensions() {
    return userExtensions;
  }
}
class JsonSchemaGenerator{

  public static void main(String[] args) throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    TypeFactory typeFactory = TypeFactory.defaultInstance();
    AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(typeFactory);
    objectMapper.getDeserializationConfig().with(introspector);
    objectMapper.getSerializationConfig().with(introspector);

    //To force mapper to include JAXB annotated properties in Json schema
    objectMapper.registerModule(new JaxbAnnotationModule());
    SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
    objectMapper.acceptJsonFormatVisitor(objectMapper.constructType(Customer.class), visitor);

    JsonSchema inputSchema = visitor.finalSchema();
    String schemaString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(inputSchema);

    System.out.println(schemaString);
  }

}
BATMAN_2008
  • 2,788
  • 3
  • 31
  • 98