Dynamic @ApiModelProperty Filtering

678 views
Skip to first unread message

Ronny Shapiro

unread,
Feb 8, 2015, 10:32:07 AM2/8/15
to swagger-sw...@googlegroups.com
Hey,

I was wondering if it was possible to filter some @ApiModel properties based on some conditions.
I'm using the same @ApiModels in different APIs but want to hide different properties in each one.
I've been trying to apply "ApiModelProperty.access" but it doesn't work on ApiModels.

As far as I know, writing my own Model Converter would make the filtering static and is out of context with calling method.

I would really like to avoid creating a different model classes for every method.

Any suggestions ?

Ron Ratovsky

unread,
Feb 8, 2015, 10:33:47 AM2/8/15
to swagger-sw...@googlegroups.com
Which version of swagger-core do you use?

--
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.



--
-----------------------------------------
http://swagger.io
https://twitter.com/SwaggerApi
-----------------------------------------

Ronny Shapiro

unread,
Feb 8, 2015, 10:38:02 AM2/8/15
to swagger-sw...@googlegroups.com
1.3.12


On Sunday, February 8, 2015 at 5:33:47 PM UTC+2, Ron wrote:
Which version of swagger-core do you use?
On Sun, Feb 8, 2015 at 5:32 PM, Ronny Shapiro <ro...@dudamobile.com> wrote:
Hey,

I was wondering if it was possible to filter some @ApiModel properties based on some conditions.
I'm using the same @ApiModels in different APIs but want to hide different properties in each one.
I've been trying to apply "ApiModelProperty.access" but it doesn't work on ApiModels.

As far as I know, writing my own Model Converter would make the filtering static and is out of context with calling method.

I would really like to avoid creating a different model classes for every method.

Any suggestions ?

--
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-swaggersocket+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Ron Ratovsky

unread,
Feb 8, 2015, 10:40:18 AM2/8/15
to swagger-sw...@googlegroups.com
I'm fairly certain it is not possible, but let's see what Tony has to say about it.

To be clear, you want to use a single model but change the model definition based on the operation itself?

You're not looking to change the definition for the model for all APIs.

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.

Ronny Shapiro

unread,
Feb 8, 2015, 10:47:09 AM2/8/15
to swagger-sw...@googlegroups.com
Exactly.
Simple example :
The model has an "ID" property which I want to hide on a POST method but want to show on a GET.

if "access" of "ApiModelProperty" was enforced I could have achieved it quite easily. I'm not sure I understand what's the use of this property otherwise. Mm I missing something here ?


On Sunday, February 8, 2015 at 5:40:18 PM UTC+2, Ron wrote:
I'm fairly certain it is not possible, but let's see what Tony has to say about it.

To be clear, you want to use a single model but change the model definition based on the operation itself?

You're not looking to change the definition for the model for all APIs.
On Sun, Feb 8, 2015 at 5:38 PM, Ronny Shapiro <ro...@dudamobile.com> wrote:
1.3.12

On Sunday, February 8, 2015 at 5:33:47 PM UTC+2, Ron wrote:
Which version of swagger-core do you use?
On Sun, Feb 8, 2015 at 5:32 PM, Ronny Shapiro <ro...@dudamobile.com> wrote:
Hey,

I was wondering if it was possible to filter some @ApiModel properties based on some conditions.
I'm using the same @ApiModels in different APIs but want to hide different properties in each one.
I've been trying to apply "ApiModelProperty.access" but it doesn't work on ApiModels.

As far as I know, writing my own Model Converter would make the filtering static and is out of context with calling method.

I would really like to avoid creating a different model classes for every method.

Any suggestions ?

--
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-swaggersocket+unsubscri...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
-----------------------------------------
http://swagger.io
https://twitter.com/SwaggerApi
-----------------------------------------

--
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-swaggersocket+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ron Ratovsky

unread,
Feb 8, 2015, 10:50:14 AM2/8/15
to swagger-sw...@googlegroups.com
There's a technical problem with doing that, since models all have a name and the name is taken from the code.
You can't have more than one model with the same name.

That said,1.5.0-M1 may offer more flexibility, but I don't know its capabilities well enough. This is where Tony would come in.

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,
Feb 8, 2015, 12:24:46 PM2/8/15
to swagger-sw...@googlegroups.com
Hi, it's not really supported to have the same model exposed with different properties.  It would be impossible to know which set of properties to show with each method, as either the input or output.

There is, however, a way to filter properties on a global level from a model.  It was introduced in 1.5.0 and can be seen here:


public interface SwaggerSpecFilter
...
boolean isPropertyAllowed(
Model model,
Property property,
String propertyName,
Map<String, List<String>> params,
Map<String, String> cookies,
Map<String, List<String>> headers);

Note that when you implement this interface and add the filter, you can decide dynamically if the property should be shown or not.  Again, you don't know the context (input/output) but this lets you keep models trim.
Reply all
Reply to author
Forward
0 new messages