如何在 Swagger 中使用自定义模板 [英] Howto use custom templates in Swagger

查看:73
本文介绍了如何在 Swagger 中使用自定义模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模板中有这个 JavaJaxRs 字典:

I have this JavaJaxRs dictionary with my templates:

/templates/JavaJaxRs

我编辑了其中一些.并希望将它们用于我的 API 生成(代码的灵感来自于 https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen-maven-plugin/src/main/java/io/swagger/codegen/plugin/CodeGenMojo.java):

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.

更新:

如果我没记错的话,我有一个条件错误:

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.

推荐答案

您可以获得代码生成器的帮助选项,如下所示:

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

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

应该告诉您可以使用 -t 参数覆盖模板位置:

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

这篇关于如何在 Swagger 中使用自定义模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆