Extending JSON schema with custom SchemaFactoryWrapper

751 views
Skip to first unread message

mkr...@trialfire.com

unread,
Dec 17, 2014, 4:37:06 PM12/17/14
to jackso...@googlegroups.com
Hi



I'm trying to extend the functionality of the basic schema generation to include properties like title.

So if I have a class like this

public class MyFoo{

 
public String foo;
 
 
public int bar;
 
 
public String baz;
}

Out of the box I can get the following json:

{
  • type"object",
  • properties
    {
    • baz
      {
      • type"string"
      },
    • foo
      {
      • type"string"
      },
    • bar
      {
      • type"integer"
      }
    }
}

 
What I'd like to do is create custom annotations so I can add JsonSchema properties to the fields in my pojo

For example if I annotate the foo property with a custom annotation (lets call it @JsonSchemaProp) like this

@JsonSchemaProp(titlePrefix = "this is a")
public String foo;

Then I'd like to see that reflected in my json schema above as

foo: 
{
  • type"string",
  • title"this is a foo",
}

I looked at the sample code in TitleSchemaFactoryWrapper but its very basic. It doesn't give me any hints on how to access the property name currently being visited and that's where I seem to be stuck. In order to accomplish my goal I need to answer these two questions:

1. How do I access the annotations on a field/class from the SchemaFactoryWrapper
2. How do Access the field currently being visted in the overridden methods of my SchemaFactoryWrapper, e.g. inside
public JsonStringFormatVisitor expectStringFormat(JavaType convertedType)


If anyone has done anything similar and can help me with 1 or 2 I'd be eternally grateful.





Tatu Saloranta

unread,
Dec 17, 2014, 7:52:36 PM12/17/14
to jackso...@googlegroups.com
I don't know if this helps, but 2.5 adds support for "virtual properties" via new @JsonAppend annotation. It comes with one default implementation (for attribute-backed properties), but also allows custom ones.

Another possibility would be to either extends JSON Schema module with annotations that could be used to decorate output with additional properties, or do it on databind-side with BeanSerializerModifier.

-+ Tatu +-


--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mkr...@trialfire.com

unread,
Dec 19, 2014, 11:54:03 AM12/19/14
to jackso...@googlegroups.com
I found what I was looking for in this pull request


This above PR adds support for JSR303 bean validation annotations to schema generation. I was able to use the code here to guide me.

Thanks!

Tatu Saloranta

unread,
Dec 19, 2014, 2:20:03 PM12/19/14
to jackso...@googlegroups.com
I went ahead and merged that one for 2.5.

-+ Tatu +-

Deepesh Aggarwal

unread,
Jan 18, 2017, 3:34:41 PM1/18/17
to jackson-user
Hi Max, 

Can you share how were you able to get modify schema based on custom annotations.

Thanks
Reply all
Reply to author
Forward
0 new messages