How to register the Model Converter with JAXRS Application

1,504 views
Skip to first unread message

Nikhil Ingole

unread,
Aug 10, 2015, 9:05:11 AM8/10/15
to Swagger
Hi,

I have a jaxrs (Apache Wink)  application and I am trying to document it with Swagger.
Swagger versions that I am using :
swagger-core 1.5.1-M1, swagger-jaxrs: 1.5.1-M1 , Swagger-UI : 2.1.8
Due to some legal reasons I wont be able to update this versions in my project, atleast for some time.

I have a Rest api that takes JsonNode (from Jackson) as input payload ( POST method). To simplify the input for users of the service, the api takes any valid json as input and internally validates if it fits this schema - { "key1" : [ "value1", "value2"] , "key2" : [ "value1", "value2"] }
Swagger UI shows the properties of JsonNode class as the model for input. I need to replace that model with the above schema.

I have implemented the model converter for this that looks like this - 
public class JsonNodeModelConverterNew extends AbstractModelConverter {

    public JsonNodeModelConverterNew(ObjectMapper mapper) {
        super(Json.mapper());
    }

    @Override
    public Model resolve(Type type, ModelConverterContext context, Iterator<ModelConverter> chain) {

        ModelImpl impl = new ModelImpl();
        String key = "<attributeName1>";
        impl.addProperty(key, context.resolveProperty(String.class));
        String value = "[ \"<attrValue1>\", \"<attrValue2>\" ]";
        impl.addProperty(value, context.resolveProperty(String.class));

        context.defineModel(impl.getName(), impl);

        return impl;
    }

    @Override
    public Property resolveProperty(Type type, ModelConverterContext context, Iterator<ModelConverter> chain) {
        return chain.next().resolveProperty(type, context, chain);
    }

But I am not able to figure out how do I a register this converter with the Jaxrs application?

I have tried adding it to the list of classes/singletons in the jaxrs Application subclass, but that does not work. Swagger UI still shows the properties of JsonNode class.

If you could point me to some example, would be great.

Another doubt that I had is - does the version 1.5.1-M1 support Collections as input parameter, Whenever I have tried it is running into ClassCastExceptions.
Even collection types present in other pojos are not correctly represented in request/response models.
What version has support for this enabled?

Any help appreciated.

Thanks,
Nikhil 


Jonathan Lemon

unread,
Aug 10, 2015, 9:35:10 AM8/10/15
to Swagger
Have you tried registering the functionality as a custom module?

I did this in my project, by extending the JacksonJaxbJsonProvider, and adding custom modules there.

Nikhil Ingole

unread,
Aug 11, 2015, 6:23:24 AM8/11/15
to Swagger
Hi,

Thanks for the reply, but I don't think I understand it completely.
Are you suggesting to write a custom serializer extending Jackson classes and then register it with ObjectMapper. ?

Because I am not sure I can add the above class to Jackson in any way. I need to plug it in Swagger somehow.

Nikhil Ingole

unread,
Aug 11, 2015, 10:44:39 AM8/11/15
to Swagger

Just to be clear, I am looking for something like https://github.com/swagger-api/swagger-core/wiki/overriding-models but for swagger 1.5.1-M1
I am not able to find the OverrideConverter converter = new OverrideConverter(); or its equivalent 
And where do I put it in a Rest application.

Thanks,
Nikhil

tony tam

unread,
Aug 11, 2015, 1:55:55 PM8/11/15
to Swagger
Reply all
Reply to author
Forward
0 new messages