FasterXML / jackson-module-jsonSchema

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

'@JsonClassDescription' annotation is not taken into account #136

Open gtbromen opened 5 years ago

gtbromen commented 5 years ago

@JsonClassDescription supported by jackson-module-jsonSchema? I have tried to generate JSON schema with jackson-module-jsonSchema 2.7.4 However, it ignores @JsonClassDescription annotation

cowtowncoder commented 5 years ago

Version 2.7.4 is very old so I would recommend upgrade to a later version, ideally 2.9(.9). If description is still not included, please file an issue (or change this one) to include reproduction of what you think should happen, ideally in form of failing unit test.

gtbromen commented 5 years ago

I try with 2.9.9 version and i have the same issue (JsonPropertyDescription annotation is ok but not JsonClassDescription annotation)

Here is the example

package test;
import com.fasterxml.jackson.annotation.JsonClassDescription;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;

@JsonClassDescription("Description of calss Test")
public class Test {

    @JsonPropertyDescription("Description of property name")
    private String name;

    public Test() {
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
        mapper.acceptJsonFormatVisitor(Test.class, visitor);
        com.fasterxml.jackson.module.jsonSchema.JsonSchema jsonSchema = visitor.finalSchema();
        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema));
    }
}

output

{
  "type" : "object",
  "id" : "urn:jsonschema:test:Test",
  "properties" : {
    "name" : {
      "type" : "string",
      "description" : "Description of property name"
    }
  }
}
cowtowncoder commented 5 years ago

Sounds like support for class description is missing then (I remember addition of property descriptions).

Contributions welcome!

cowtowncoder commented 5 years ago

I checked that code indeed does not fetch this information. It would be available from jackson-databind side using BeanDescription.findClassDescription(), but wiring does not exist. This is different from description for properties which get access via JsonSchema.enrichWithBeanProperty() (using beanProperty.getMetadata().getDescription()).

So: someone would need to connect the dots. I do not work on this module, but maybe someone else has time and interest.

bmogensen commented 4 years ago

I decided to use the Swagger annotations instead with:

@ApiModel(
        value = "Party",
        description = "An organization or person")
public class Party {
...
}

Create the Swagger module and add it to config builder:

SwaggerModule swaggerModule = new SwaggerModule();
...
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(objectMapper,SchemaVersion.DRAFT_2019_09,OptionPreset.PLAIN_JSON).with(jacksonModule).with(swaggerModule)
Praytic commented 2 years ago

I think, as a part of this effort, @JsonPropertyDescription annotation can also be taken into the account.

mdedetrich commented 1 year ago

I think, as a part of this effort, @JsonPropertyDescription annotation can also be taken into the account.

@JsonPropertyDescription's annotation only works on fields and I think it makes sense to keep it this way. Its probably best to update @JsonClassDescription so that it does actually work as intuitively intended.

cowtowncoder commented 1 year ago

Added pr-needed since I won't have any time to work on this. But it seems like a valid idea (within constraints that this module is itself unsupported ... but is released for Jackson 2.x, PRs accepted etc)