-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ignore ApiParam. #167
Ignore ApiParam. #167
Conversation
There are several scenarios, which may necessitate a parameter to be excluded from the documentation. This works by adding an ignore flas the ApiParam annotation and then checking for it when creating the documentation.
@fehguy any interest on this? just wondering... |
Yes, getting there, and making sure it works across the models, etc. |
Would love this! |
The |
see here, this is supported in the 1.3-develop branch, which will be pushed to master shortly: and the corresponding filter logic: |
Great! When do you expect 1.3 will be in Maven Central? |
there will be a snapshot this week |
added support in 1.3.0-SNAPSHOT. You create a filter and ignore whatever you like from params: class SecretParamFilter extends SwaggerSpecFilter {
override def isOperationAllowed(operation: Operation, api: ApiDescription, params: java.util.Map[String, java.util.List[String]], cookies: java.util.Map[String, String], headers: java.util.Map[String, java.util.List[String]]): Boolean = true
override def isParamAllowed(parameter: Parameter, operation: Operation, api: ApiDescription, params: java.util.Map[String, java.util.List[String]], cookies: java.util.Map[String, String], headers: java.util.Map[String, java.util.List[String]]): Boolean = {
if(parameter.paramAccess == Some("secret")) false
else true
}
} |
Awesome! I really appreciate this. Can you post the maven coordinates? I wasn't able to find it in maven central. Thanks again, |
it's a snapshot, so you can find it in sonatype snapshots: |
What about swagger-jaxrs? I don't see a 1.3.0 snapshot for that. In fact, it seems jaxrs support is back in version 2.8.1. Should I not be using swagger-jaxrs? |
Man, I'm so sorry to keep bugging you. What happened to JaxrsApiReader.setFormatString()? I can't find anything on why it was removed. |
Hey sorry for not replying. It was removed because of #123 which is something that everyone in the known universe has asked us. So there's no need to set the format string, because it is no longer there in 1.3.0-SNAPSHOT (and beyond) |
Haha. Great! One less line of code for me. :) |
Do you have an upgrade guide to 1.3.0? Once I switched, there's no data being returned from the documentation endpoint. It finds it, and it just returns {}. |
Guide isn't done yet but I can tell you that issue is because you don't have the swagger JSON providers in your scope. You can add those with the web.xml by scanning the com.wordnik.swagger.jaxrs.listing package: <init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.wordnik.swagger.jaxrs.listing</param-value>
</init-param> By programmatically adding them, as in the case of Dropwizard: @Override
public void run(SwaggerSampleConfiguration configuration, Environment environment) {
environment.addResource(new ApiListingResourceJSON());
environment.addResource(new PetResource());
environment.addProvider(new ApiDeclarationProvider());
environment.addProvider(new ResourceListingProvider());
ScannerFactory.setScanner(new DefaultJaxrsScanner());
ClassReaders.setReader(new DefaultJaxrsApiReader());
} Or through configuration, as with Apache-CXF: <bean id="resourceWriter" class="com.wordnik.swagger.jaxrs.listing.ResourceListingProvider" />
<bean id="apiWriter" class="com.wordnik.swagger.jaxrs.listing.ApiDeclarationProvider" />
<bean class="org.apache.cxf.jaxrs.JAXRSServerFactoryBean" init-method="create">
<property name="address" value="/" />
<property name="serviceBeans">
<list>
<!-- the listing resource -->
<ref bean="swaggerResourceJSON" />
</list>
</property>
<property name="providers">
<list>
<!-- required for writing swagger classes -->
<ref bean="resourceWriter" />
<ref bean="apiWriter" />
</list>
</property>
</bean> |
I have created a custom filter which implements SwaggerSpecFilter to filter out apiParams injected by framework. public class DwSwaggerInternalFilter implements SwaggerSpecFilter {
} My code runs inside Dropwizard.
How do I add my filter into swagger infrastructure? Thanks! |
tmazukna, were you able to figure out how to register a SwaggerSpecFilter within a DropWizard project? |
Yes, this is how to do this: FilterFactory.filter_$eq( new DwSwaggerInternalFilter() ); filter is defined as a property in Scala, but in JAva you see this strange method signature for a setter ;) Tomas |
Great! Thanks Tomas, that works perfectly. Peter |
Thanks Tomas. Your solution worked perfectly... Appreciate you shared the solution.... Shalini |
This is pushed in 1.3.3-SNAPSHOT for both @ApiOperation and @ApiModelProperty. There is now a hidden property on both annotations that allow you to filter away fields and methods. |
@fehguy just checking - but should the hidden=true flag also be added to @ApiParam? Or should we continue to write custom filters for those? |
Hi, I believe you can now use hidden=true instead of the custom filter. Both should work |
Tried this to no effect. Is there a certain version of swagger core and swagger ui you need to be on for this? Sent from my iPhone
|
For hidden=true, you need swagger-core 1.3.4. swagger-ui version is irrelevant. |
@fehguy Is hidden=true available in ApiParam now? I'm on 1.3.10 and don't see it. Having that option would be much preferred to standing up a servlet filter just for this one feature (especially in JAX-RS deployments that aren't already using servlets -- like those on Grizzly). Or, if that's not an option, is there some guidance on how to deploy a SwaggerSpecFilter as a JAX-RS 2.0 ContainerResponseFilter? (That would probably be good to know anyway for other uses of "access"). Thanks! |
it looks like there isn't a hidden field in a parameter, only in ApiOperation and ApiModelProperties. If you want to remove a parameter, you can simply create a filter like such: Note implementing your own logic for the |
@fehguy Thanks for the reply! I did see that mentioned in the thread, but it wasn't entirely obvious how to do this for a JAX-RS 2.0 app that's not using servlets. I ended up implementing a ContainerResponseFilter (see gist) which appears to work. However, is it expected that I would need to interact directly with SpecFilter.filter() this way? Thanks again for your feedback! |
You shouldn't need to handle it on your own--the swagger framework will apply the filter on it's own. See here how it's configured: <init-param>
<param-name>swagger.filter</param-name>
<param-value>com.wordnik.swagger.sample.util.ApiAuthorizationFilterImpl</param-value>
</init-param> |
DefaultJaxrsConfig seems to expect a servlet (which I don't have). Is there a way to set the I'm currently initializing Swagger with the following code in my Main.java class: SwaggerConfig swaggerConfig = new SwaggerConfig();
ConfigFactory.setConfig(swaggerConfig);
swaggerConfig.setSwaggerVersion("2.0");
swaggerConfig.setBasePath(BASE_URI);
swaggerConfig.setApiVersion("1.0.0");
ScannerFactory.setScanner(new DefaultJaxrsScanner());
ClassReaders.setReader(new DefaultJaxrsApiReader()); Thanks! |
@pofallon - Yes. You can use the BeanConfig class to set the filter. |
There are several scenarios which may necessitate a parameter to be excluded from the documentation.
This works by adding an "ignore" flag to the ApiParam annotation and then checking for it when creating the documentation.