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

'@JsonClassDescription' annotation is not taken into account

Open gtbromen opened this issue 5 years ago • 8 comments

@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

gtbromen avatar Jun 14 '19 09:06 gtbromen

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.

cowtowncoder avatar Jun 14 '19 18:06 cowtowncoder

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"
    }
  }
}

gtbromen avatar Jun 17 '19 08:06 gtbromen

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

Contributions welcome!

cowtowncoder avatar Jun 18 '19 18:06 cowtowncoder

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.

cowtowncoder avatar Aug 10 '19 03:08 cowtowncoder

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)

bmogensen avatar May 16 '20 12:05 bmogensen

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

Praytic avatar Jul 12 '22 20:07 Praytic

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.

mdedetrich avatar Nov 09 '23 07:11 mdedetrich

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)

cowtowncoder avatar Nov 09 '23 18:11 cowtowncoder