swagger 2.0 adding custom ModelConverters

1,219 views
Skip to first unread message

Tim Van der Wee

unread,
Dec 18, 2014, 6:12:48 AM12/18/14
to swagger-sw...@googlegroups.com
I'm looking into swagger-core 1.5.0 and seem to be missing the possibility to add custom ModelConverters.  There used to be an addConverter method on MethodConverters, is there another way to register custom model converters starting from 1.5? 

Kr,

Tim

Ron

unread,
Dec 18, 2014, 6:19:38 AM12/18/14
to swagger-sw...@googlegroups.com
Hi Tim,

I'm not familiar enough with 1.5 yet, however, the model building mechanism has moved to Jackson entirely (https://github.com/FasterXML/jackson-module-swagger).
As such, I imagine that any customizations that are available to Jackson would be available here to create custom models.

Tony should be able to provide more information later.

Ron

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

tony tam

unread,
Dec 19, 2014, 7:16:10 AM12/19/14
to swagger-sw...@googlegroups.com
Hi, as Ron mentioned, the cleanest way to do custom model conversion is with Jackson annotations.  There is an interface for making a custom converter but the implementatin is not there.

I will note that the Jackson swagger code has been generously donated from the Jackson folks back to the swagger project, and is no longer a dependency.  The code is in the swagger-core module.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

tvdwee

unread,
Dec 19, 2014, 7:57:28 AM12/19/14
to swagger-sw...@googlegroups.com
Hi Tony,Ron,

I'm afraid the Jackson annotations don't provide enough flexibility for our use case. Without going into all details an example of what we do.

Given a class we want to expose through our api:

Class Entity
+String id
+String name
+Map<String,Object> propertySetA 
+Map<String,Object> propertySetB
+Map<String,Object> propertySetB

make it look like



we need to:
1. Add the properties in the various sets to the model as property because the API user doesn't need to know about the propertySets A B C, it's an implementation detail, make it look like the properties belong to the entity.
2. Add Swagger description on all properties in the sets A B C. These descriptions (and types) are stored in a metadata repository

I'll see how hard it is to complete the custom converter implementation and send you a pull request so you can have a look.

Tim 

tony tam

unread,
Dec 19, 2014, 8:01:22 AM12/19/14
to swagger-sw...@googlegroups.com
Thanks Tim, that would be helpful.  The goal is to support it, it just hasn't been implemented yet.

I believe the place to go would be in the ModelResolver.java and add a method in ModelConverter.java (possibly an abstract class) that returns the right POJO for the input class.

tvdwee

unread,
Dec 23, 2014, 6:20:11 PM12/23/14
to swagger-sw...@googlegroups.com
I refactored the ModelConverters in such a way that there is an AbstractModelConverter which serves as base for custom modelConverters.  The custom converter can delegate resolving of Models and Properties to the existing ModelConverters.  

https://github.com/swagger-api/swagger-core/pull/807

Kr,

Tim

Reply all
Reply to author
Forward
0 new messages