Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use custom templates in Swagger

I have this JavaJaxRs dictionary with my templates:

/templates/JavaJaxRs

I edited some of them. And want to use them for my API generation (Code was inspired from this approach from https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen-maven-plugin/src/main/java/io/swagger/codegen/plugin/CodeGenMojo.java):

    System.out.println("Generating API for: " + location);
    DefaultGenerator generator = new DefaultGenerator();
    Swagger swagger = new SwaggerParser().read(location);
    CodegenConfig config = CodegenConfigLoader.forName(this.language);
    config.setOutputDir(new File(this.apiGeneratedSrcPath).getPath());

    if (null != templateDirectory) {
        config.additionalProperties().put(TEMPLATE_DIR_PARAM, templateDirectory);
    }
    if (null != modelPackage) {
        config.additionalProperties().put(MODEL_PACKAGE_PARAM, modelPackage);
    }
    if (null != apiPackage) {
        config.additionalProperties().put(API_PACKAGE_PARAM, apiPackage);
    }
    if (null != invokerPackage) {
        config.additionalProperties().put(INVOKER_PACKAGE_PARAM, invokerPackage);
    }

    if (configOptions != null) {
        for (CliOption langCliOption : config.cliOptions()) {
            if (configOptions.containsKey(langCliOption.getOpt())) {
                config.additionalProperties().put(langCliOption.getOpt(),
                        configOptions.get(langCliOption.getOpt()));
            }
        }
    }

    if (null != configurationFile) {
        Config genConfig = ConfigParser.read(configurationFile);
        if (null != genConfig) {
            for (CliOption langCliOption : config.cliOptions()) {
                if (genConfig.hasOption(langCliOption.getOpt())) {
                    config.additionalProperties().put(langCliOption.getOpt(), genConfig.getOption(langCliOption.getOpt()));
                }
            }
        } else {
            throw new RuntimeException("Unable to read configuration file");
        }
    }

    ClientOptInput input = new ClientOptInput().opts(new ClientOpts()).swagger(swagger);
    input.setConfig(config);

    generator.opts(input).generate();

Somehow i always get the code generated with the standard template file.

UPDATE:

When i remember correctly i had a conditional bug on:

if(null != templateDirectory)
    config.additionalProperties().put(TEMPLATE_DIR_PARAM, templateDirectory);

or somewhere else but with the right condition, code was working as intended.

I let the question stay here, maybe it will help some other users.

like image 734
Gobliins Avatar asked Oct 30 '15 12:10

Gobliins


1 Answers

You can get the help options for the code generator like such:

java -jar swagger-codegen-cli.jar help generate

Which should tell you that you can override the template location with the -t parameter:

java -java swagger-codegen-cli.jar generate -l {language} -t path/to/templates
like image 142
fehguy Avatar answered Sep 26 '22 15:09

fehguy