kongchen / swagger-maven-plugin

JAX-RS & SpringMVC supported maven build plugin, helps you generate Swagger JSON and API document in build phase.
http://kongchen.github.io/swagger-maven-plugin/
Apache License 2.0
761 stars 450 forks source link

How to use overriden SwaggerSchemaConverter #73

Open FangmingD opened 10 years ago

FangmingD commented 10 years ago

I have a class to override SwaggerSchemaConverter for generating the swagger models. It's working with my rest api and swagger ui.

like

public class CustomConverter extends SwaggerSchemaConverter {

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

        LinkedHashMap<String,ModelProperty> props = old.properties();
        props = (LinkedHashMap<String, ModelProperty>) props.$minus("_persistence_shouldRefreshFetchGroup");

        Model m = new Model(old.id(),old.name(), old.qualifiedType(),props,old.description(), old.baseModel(),old.discriminator(),old.subTypes());
        Option<Model> nopt = Option.apply(m);
        return nopt;
    }

}

but I'm not sure how this class can be called through this mavn-swagger plugin

FangmingD commented 10 years ago

I think that one uses com.wordnik.swagger.converter.ModelConverters to config the Swagger as following ModelConverters.addConverter(new CustomConverter(), true)

inekrashevych commented 9 years ago

Here is the workaround I've found for this: 1) Implement "true" SwaggerFilter and add the ModelConverters.addConverter in constructor with true as second argument (this will add it as first converter):

// ommitted the imports
public class SwaggerFilter implements SwaggerSpecFilter {
    private static final Logger LOGGER = LoggerFactory.getLogger(SwaggerFilter.class);
    public SwaggerFilter() {
        LOGGER.debug("Adding Model Converter.");
        // adding model converter you have there
        // the lines below will add this converter to the swagger as the first converter to use in the list
        ModelConverters.addConverter(new CustomConverter(), true);
    }

    @Override
    public boolean isOperationAllowed(Operation operation, ApiDescription apiDescription,
            Map<String, List<String>> params, Map<String, String> cookies,
            Map<String, List<String>> headers) {
        // return true to allow any operations
        return true;
    }

    @Override
    public boolean isParamAllowed(Parameter parameter, Operation op, ApiDescription api,
            Map<String, List<String>> params, Map<String, String> cookies,
            Map<String, List<String>> headers) {
        // return true to allow any parameters
        return true;
    }

}

2) Add the filter to the maven config:

              <swaggerInternalFilter>your.company.SwaggerFilter</swaggerInternalFilter>

Also Note that latest (currently I'm using v2.3) is using different version of swagger core that you've mentioned (in the code you have you're implementing the class from the different swagger-core version). So, that latest CustomConverter read override will look like:

// .....
public class CustomConverter extends SwaggerSchemaConverter {
// .....
@Override
    public Option<Model> read(Class<?> cls, Map<String, String> typeMap) {
// implement here
    }
}