Custom MessageBodyWriter - Jersey JAX-RS Swagger

704 views
Skip to first unread message

jonathan...@safe.com

unread,
Aug 23, 2013, 1:19:26 PM8/23/13
to swagger-sw...@googlegroups.com
Does Swagger support custom MessageBodyWriter or MessageBodyReader implementations?  I am using a custom XML/JSON reader/writer that unifies the two formats with very little annotation clutter or fuss.  JAXB, Jackson and MOXy simply do not work well-enough for my purposes unfortunately.  So far it looks like Swagger doesn't recognize these custom implementations, but am I missing something?  It appears that JAXB annotations are also ignored (such as @XmlTransient and @JsonIgnore).

tony tam

unread,
Aug 23, 2013, 1:32:42 PM8/23/13
to swagger-sw...@googlegroups.com
You're talking about for introspecting models?  Swagger doesn't get in the way of *any* resources for reading or writing any format except the swagger json files.  This is, of course, if you're using 1.3.0.

jonathan...@safe.com

unread,
Aug 23, 2013, 2:31:57 PM8/23/13
to swagger-sw...@googlegroups.com
Yes I am refering to models and yes I am using 1.3.0.  So may I ask how does swagger come-up with the list of properties on a pojo in an API response?

jonathan...@safe.com

unread,
Aug 23, 2013, 2:35:45 PM8/23/13
to swagger-sw...@googlegroups.com
Furthermore, is there a way to control how this list of properties is generated? (i.e. programmatically rename some properties, hide others)

tony tam

unread,
Aug 23, 2013, 2:41:36 PM8/23/13
to swagger-sw...@googlegroups.com
Swagger has a property introspector.  You can write your own to support writers very easily--the default is here:


You can register any model converter like such:


ModelConverters.addConverter(converter, true)

Where converter is a class which extends ModelConverter:

trait ModelConverter {
  def read(cls: Class[_]): Option[Model]
  def toName(cls: Class[_]): String
  def toDescriptionOpt(cls: Class[_]): Option[String]

  def ignoredPackages: Set[String] = Set("java.lang")
  def ignoredClasses: Set[String] = Set("java.util.Date")
}

The typical way to override models without writing your own introspecter is documented here:

jonathan...@safe.com

unread,
Aug 23, 2013, 4:21:59 PM8/23/13
to swagger-sw...@googlegroups.com
Thanks, I'll take a look and get back to you!

jonathan...@safe.com

unread,
Aug 27, 2013, 6:42:47 PM8/27/13
to swagger-sw...@googlegroups.com
I've implemented a custom converter in Scala and integrated it into my project.  Everything looks good except for handling enum types and container types, but I will post a new thread for the container issue since I believe it is unrelated to this topic.

My custom converter populates allowable values for enum-type properties, but it looks like Swagger-UI and the JSON schema do not honor enum types in general.

Here is my code for populating allowable values of a property:

ModelProperty(
  ...
  allowableValues = {
    if (`type`.isEnum)          // `type` is my Java property type of type Class<?>
      AllowableListValues((for(v <- `type`.getEnumConstants) yield v.toString).toList)
    else
      AnyAllowableValues
    },
    ...
)

The model shows the property type as "undefined" and does not show the list of allowable values.  I think the property type is expressed as the actual enum Java class type, but instead should it be an array of strings?

tony tam

unread,
Sep 3, 2013, 1:45:21 PM9/3/13
to swagger-sw...@googlegroups.com

Hi Jonathan, have you tried with the latest swagger-ui (2.0.1)?  There was a fix for the "undefined" type.  Or are you saying this occurs in the JSON itself?

Reply all
Reply to author
Forward
0 new messages