Model annotation - filter not working?

1,239 views
Skip to first unread message

Andreas Fey

unread,
Nov 28, 2013, 5:52:39 AM11/28/13
to swagger-sw...@googlegroups.com
Hi,
while I got filtering API parameters to work, I do not see any possibility to get filters working when annotating my models like


As the annotation @ApiModelProperty claims, the "access" key lets you filter the models property like the parameters of the API. But the filter class is not called for the model class at all. So, how to filter model attributes? It is very ugly to show our customers the full content of a model.

Best,

Andreas

tony tam

unread,
Nov 28, 2013, 6:18:05 PM11/28/13
to swagger-sw...@googlegroups.com
The filtering would be done in a custom model processor, not at runtime.  Sorry if this is not clear in the docs

Andreas Fey

unread,
Nov 29, 2013, 12:04:45 AM11/29/13
to swagger-sw...@googlegroups.com
OK, is there any docu how to set a custom model processor?

Andreas Fey

unread,
Nov 29, 2013, 1:44:41 AM11/29/13
to swagger-sw...@googlegroups.com
Got it. My converter class looks like:

public class SwaggerModelConverter extends SwaggerSchemaConverter
{

@Override
public Option<Model> read( Class<?> cls )
{
Option<Model> ret = super.read( cls );

if ( AbstractModel.class.isAssignableFrom( cls ) )
{
Class<?> clazz = cls;
while ( clazz.getSuperclass( ) != null )
{
for ( Field f : cls.getDeclaredFields( ) )
{
try
{
if ( f.isAnnotationPresent( ApiModelProperty.class ) )
{
if ( f.getAnnotation( ApiModelProperty.class ).access( ).isEmpty( ) == false )
{
ret.get( ).properties( ).remove( f.getName( ) );
}
}
}
catch ( SecurityException e )
{
Logger.getLogger( getClass( ) ).error( "Error parsing model fields for swagger", e );
}
}
clazz = clazz.getSuperclass( );
}
}
return ret;
}
}

And one has only to declare it once a time like:
ModelConverters.addConverter( new SwaggerModelConverter( ), true );

Its working now. Altough I expected that this basic bahavour is implemented by default.

tony tam

unread,
Nov 29, 2013, 4:31:38 AM11/29/13
to swagger-sw...@googlegroups.com
Glad you got it working

R Allen

unread,
Dec 19, 2013, 1:19:10 PM12/19/13
to swagger-sw...@googlegroups.com
Bug in this code
for ( Field f : cls.getDeclaredFields( ) )
should be
for ( Field f : clazz.getDeclaredFields( ) )

Also for others, the required java includes are
import com.wordnik.swagger.annotations.ApiModelProperty;
import com.wordnik.swagger.converter.ModelConverters;
import com.wordnik.swagger.converter.SwaggerSchemaConverter;
import com.wordnik.swagger.model.Model;
import scala.Option;

The AbstractModel referenced in example is specific to Mr. Fey's environment. I'm using package name matching to select classes of interest.

Guillaume Simard

unread,
Mar 14, 2014, 2:44:35 PM3/14/14
to swagger-sw...@googlegroups.com
This code snippet was useful to me. I needed to overwrite the default behavior of documenting every member whether they are decorated with @ApiModelProperty or not.

satheesh kumar

unread,
Apr 18, 2014, 11:58:35 AM4/18/14
to swagger-sw...@googlegroups.com
I had to overwrite my model behavior. I used the above example in my application. But where should I configure this converter class so that it will be invoked?

I see Am mentioned to add this piece of code.

ModelConverters.addConverter( new SwaggerModelConverter( ), true );

But I'm not sure where this shud be added?Can anyone please help me on this?

Tommaso Bianco

unread,
May 11, 2014, 7:10:55 AM5/11/14
to swagger-sw...@googlegroups.com
The same goes for me Sat, how to let this SwaggerSchemaConverter be invoked. Did you resolve the problem?

Tommaso Bianco

unread,
May 12, 2014, 2:18:16 PM5/12/14
to swagger-sw...@googlegroups.com
I resolved this issue.
In terms of application, I added a ServletContextListener, because my example works with automatic package scanning, and not with an Application main class from which to instantiate stuff.
This Listener add my custom converter, ModelConverters.addConverter(new ApiSpecModelConverter(), true);
The diff for this commit is this :
https://github.com/atomobianco/java-jersey2-petstore/commit/003cadd1bad061e22ca3368308c076fa9ea9ad1b

In terms of model converter, I managed to filter out an ApiModelProperty (type) on a particular derived class (CollectionResource) and leave it on other classes. The hidden=true wasn't working in this case.
I had to manage letting swagger to imagine having a field where actually I had just the getField method (because my resources just LinkedHashMap)
The diff from the previous version is here:
https://github.com/atomobianco/java-jersey2-petstore/commit/7ee37fb6e39a11357e32e06a30482735fada8e64

Hope this will help!

Trever Pietsch

unread,
Jun 6, 2014, 2:18:38 AM6/6/14
to swagger-sw...@googlegroups.com

Hi Everybody,

im not sure if this is what i am looking for but I don't want the whole model to be recursively displayed (as some of the grails variables get called until they resolve to object)

how can i do this
Screen Shot 2014-06-05 at 11.16.27 PM.png

tony tam

unread,
Jun 6, 2014, 2:50:06 AM6/6/14
to swagger-sw...@googlegroups.com
Hi, I think those are the actual models you're exposing.  What are you looking for exactly?
Reply all
Reply to author
Forward
0 new messages