Skip to content
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

Closed
wants to merge 1 commit into from
Closed

Ignore ApiParam. #167

wants to merge 1 commit into from

Conversation

earaya
Copy link

@earaya earaya commented Apr 3, 2013

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.

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.
@earaya
Copy link
Author

earaya commented Apr 19, 2013

@fehguy any interest on this? just wondering...

@fehguy
Copy link
Contributor

fehguy commented Apr 19, 2013

Yes, getting there, and making sure it works across the models, etc.

@znbailey
Copy link

Would love this!

@fehguy
Copy link
Contributor

fehguy commented May 24, 2013

The access() attribute is intended for this purpose. Custom filtering based on access is going in right now.

@ghost ghost assigned fehguy May 24, 2013
@fehguy
Copy link
Contributor

fehguy commented May 28, 2013

@earaya
Copy link
Author

earaya commented May 30, 2013

Great! When do you expect 1.3 will be in Maven Central?

@fehguy
Copy link
Contributor

fehguy commented May 30, 2013

there will be a snapshot this week

@fehguy
Copy link
Contributor

fehguy commented May 31, 2013

added support in 1.3.0-SNAPSHOT. You create a filter and ignore whatever you like from params:

https://github.com/wordnik/swagger-core/blob/develop-1.3/modules/swagger-core/src/test/scala/filter/SpecFilterTest.scala#L44

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

@fehguy fehguy closed this May 31, 2013
@earaya
Copy link
Author

earaya commented Jun 1, 2013

Awesome! I really appreciate this. Can you post the maven coordinates? I wasn't able to find it in maven central.

Thanks again,
Esteban

@fehguy
Copy link
Contributor

fehguy commented Jun 1, 2013

it's a snapshot, so you can find it in sonatype snapshots:

https://oss.sonatype.org/content/repositories/snapshots/com/wordnik/swagger-core_2.9.1/1.3.0-SNAPSHOT/

@earaya
Copy link
Author

earaya commented Jun 4, 2013

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?

@earaya
Copy link
Author

earaya commented Jun 5, 2013

Man, I'm so sorry to keep bugging you. What happened to JaxrsApiReader.setFormatString()? I can't find anything on why it was removed.

@fehguy
Copy link
Contributor

fehguy commented Jun 5, 2013

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)

@earaya
Copy link
Author

earaya commented Jun 6, 2013

Haha. Great! One less line of code for me. :)

@earaya
Copy link
Author

earaya commented Jun 6, 2013

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

@fehguy
Copy link
Contributor

fehguy commented Jun 6, 2013

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:

https://github.com/wordnik/swagger-core/blob/develop-1.3/samples/java-jaxrs/src/main/webapp/WEB-INF/web.xml#L7

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

https://github.com/wordnik/swagger-core/blob/develop-1.3/samples/java-dropwizard/src/main/java/com/wordnik/swagger/sample/SwaggerSampleService.java#L28

  @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:

https://github.com/wordnik/swagger-core/blob/develop-1.3/samples/java-jaxrs-cxf/src/main/resources/applicationContext.xml#L16

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

@tmazukna
Copy link

I have created a custom filter which implements SwaggerSpecFilter to filter out apiParams injected by framework.

public class DwSwaggerInternalFilter implements SwaggerSpecFilter {

@Override
public boolean isOperationAllowed(Operation operation, ApiDescription apiDescription, Map<String, List<String>> stringListMap, Map<String, String> stringStringMap, Map<String, List<String>> stringListMap2) {
    return true;
}

@Override
public boolean isParamAllowed(Parameter parameter, Operation operation, ApiDescription apiDescription, Map<String, List<String>> stringListMap, Map<String, String> stringStringMap, Map<String, List<String>> stringListMap2) {
    if( parameter.paramAccess().isDefined() && parameter.paramAccess().get().equals("apiExclude") )
        return false;
    return true;
}

}

My code runs inside Dropwizard.
Here is my bundle run:

@Override
public void run(T dwConfiguration, Environment environment) {

    // Set the swagger config options
    SwaggerConfig config = ConfigFactory.config();
    DwSwaggerConfig configuration = findSwaggerConf(dwConfiguration);
    if( configuration != null ) {
        config.setApiVersion( configuration.getApiVersion() );
        config.setBasePath( configuration.getBasePath() );
    }

    // CORS Support for api-docs
    environment.addFilter(CrossOriginFilter.class, "/*");

    // Swagger Resource
    environment.addResource(new ApiListingResourceJSON());

    // Swagger providers
    environment.addProvider(new ApiDeclarationProvider());
    environment.addProvider(new ResourceListingProvider());

    // Swagger Scanner, which finds all the resources for @Api Annotations
    ScannerFactory.setScanner(new DefaultJaxrsScanner());

    // Add the reader, which scans the resources and extracts the resource information
    ClassReaders.setReader(new DefaultJaxrsApiReader());

}

How do I add my filter into swagger infrastructure?

Thanks!

@peterstrapp
Copy link

tmazukna, were you able to figure out how to register a SwaggerSpecFilter within a DropWizard project?

@tmazukna
Copy link

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 ;)
This is in 2.10 version of swagger.
I opened a change request with swagger team to fix the api, so later versions may have a proper setter and getter.

Tomas

@peterstrapp
Copy link

Great! Thanks Tomas, that works perfectly.

Peter

@shalucsekaran
Copy link

Thanks Tomas. Your solution worked perfectly... Appreciate you shared the solution....

Shalini

@fehguy
Copy link
Contributor

fehguy commented Mar 15, 2014

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.

@apemberton
Copy link

@fehguy just checking - but should the hidden=true flag also be added to @ApiParam? Or should we continue to write custom filters for those?

@fehguy
Copy link
Contributor

fehguy commented Apr 21, 2014

Hi, I believe you can now use hidden=true instead of the custom filter. Both should work

@apemberton
Copy link

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

On Apr 21, 2014, at 12:19 AM, Tony Tam notifications@github.com wrote:

Hi, I believe you can now use hidden=true instead of the custom filter. Both should work


Reply to this email directly or view it on GitHub.

@webron
Copy link
Contributor

webron commented Apr 27, 2014

For hidden=true, you need swagger-core 1.3.4. swagger-ui version is irrelevant.

@pofallon
Copy link
Contributor

@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!

@fehguy
Copy link
Contributor

fehguy commented Oct 18, 2014

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:

https://github.com/wordnik/swagger-core/blob/master/samples/java-jaxrs/src/main/java/com/wordnik/swagger/sample/util/ApiAuthorizationFilterImpl.java#L60

Note implementing your own logic for the isParamAllowed will make the field visible or not.

@pofallon
Copy link
Contributor

@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!

@fehguy
Copy link
Contributor

fehguy commented Oct 20, 2014

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:

https://github.com/wordnik/swagger-core/blob/master/samples/java-jaxrs/src/main/webapp/WEB-INF/web.xml#L44-L47

    <init-param>
      <param-name>swagger.filter</param-name>
      <param-value>com.wordnik.swagger.sample.util.ApiAuthorizationFilterImpl</param-value>
    </init-param>

@pofallon
Copy link
Contributor

DefaultJaxrsConfig seems to expect a servlet (which I don't have). Is there a way to set the swagger.filter property in an environment without any servlets or web.xml?

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!

@webron
Copy link
Contributor

webron commented Oct 25, 2014

@pofallon - Yes. You can use the BeanConfig class to set the filter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants