reinert / JJSchema

A generator from Java Types to JSON-Schema using Jackson.
Other
117 stars 56 forks source link

Question: object mapper with custom configuration for schema generation #96

Closed valasatava closed 5 years ago

valasatava commented 5 years ago

Is it possible to provide custom configuration for object mapper used for schema generation?

reinert commented 5 years ago

Yes, you can directly access the ObjectMapper responsible for generating the schemes through SchemaWrapperFactory.MAPPER. Be careful, though, to use JsonSchemaV4Factory instead of JsonSchemaGeneratorV4 which is deprecated.

valasatava commented 5 years ago

it doesn't seem to work with SchemaWrapperFactory.MAPPER.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); my interest in having a lower underscore case format in the resulting JSON schema.

reinert commented 5 years ago

Please, post your code so I can tell you better what to do.

valasatava commented 5 years ago

Sorry it took me so long to provide more information. Here is my code:

JsonSchemaFactory schemaFactory = new JsonSchemaV4Factory(); schemaFactory.setAutoPutDollarSchema(true); JsonNode schema = schemaFactory.createSchema(Entry.class);

I'd linke to configure the mapper to use snake case naming strategy.

reinert commented 5 years ago

Where is your mapper?

reinert commented 5 years ago

If you're using JAX-RS with some Jackson integration, than you need to configure the mapper based on Jackson's jax-rs docs.

valasatava commented 5 years ago

It's a simple Java application with dependences on Jackson libraries. I can create and configure the mapper but it seems that JsonSchemaV4Factory relies on it's own mapper internally. What's the mechanism to alter it's behavior?

reinert commented 5 years ago

I see. It seems Jackson converts only Java POJOs to configured case but doesn't behavior similarly with JsonNodes (and other objects).

JJSchema produces JsonNodes in camel case by default. The generated JsonNode should be converted to snake case manually or with some other library like guava or gson.

Please refer to this SO thread for a deeper explanation.

valasatava commented 5 years ago

Thank you very much for you prompt responses and references. I'll proceed from there.

reinert commented 5 years ago

As a test execute the following code:

ObjectMapper MAPPER = new ObjectMapper();
MAPPER.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);

ObjectNode node = MAPPER.createObjectNode();
node.put("camelCase", "test");

System.out.println(MAPPER.writeValueAsString(node));

and it will produce: {"camelCase":"test"}

As we can see, Jackson didn't apply naming strategy to a generated JsonNode.