在胡子模板中访问Swagger Codegen模型var

时间:2018-11-23 11:38:12

标签: swagger swagger-codegen swagger-codegen-maven-plugin

我正在尝试使用摇摇欲坠的代码生成器(版本2.3.1)创建我的第一个自定义代码生成器。我的问题是,我似乎无法访问模型的“变量”。我使用petstore.json示例作为输入,并使用了swagger-codegen-maven-plugin创建生成的源。

在我的胡子模板中,我尝试访问model properties。这适用于“ className”或“ package”之类的属性,但是我无法访问列表类型属性“ vars”。使用“ -DdebugModels”运行时,将显示var,并且还将“ hasVars”报告为true。但是在生成的代码中未使用var,并且“ hasVars”似乎是错误的。我已经简化了模板以在此处显示。首先是模板:

package {{package}};
{{classname}}
{{#vars}}
{{{datatype}}} {{name}} = {{{defaultValue}}}
{{/vars}}
{{#hasVars}}
hasVars
{{/hasVars}}
{{^hasVars}}
has No Vars
{{/hasVars}}

这是Pet的输出示例:

package com.dukescripd.demo.model;
Pet
has No Vars

我从stubs generated by codegen开始了我的项目。这会生成一些小胡子模板,其中包括一个用于api的模板。此模板使用“操作”列表类型属性,效果很好。

这是我的CodeGenerator:

package com.dukescript.swagger.codegen;

import io.swagger.codegen.*;

import java.util.*;
import java.io.File;

public class DukescriptswaggercodegenGenerator extends DefaultCodegen implements CodegenConfig {

    protected String sourceFolder = "src";
    protected String apiVersion = "1.0.0";

    public CodegenType getTag() {
        return CodegenType.CLIENT;
    }

    public String getName() {
        return "DukeScriptSwaggerCodegen";
    }

    public String getHelp() {
        return "Generates a DukeScript @Model client library.";
    }

    public DukescriptswaggercodegenGenerator() {
        super();

        outputFolder = "generated-code/DukeScriptSwaggerCodegen";

        modelTemplateFiles.put(
                "model.mustache", 
                "VMD.java");      


        apiTemplateFiles.put(
                "api.mustache", 
                ".java");       

        templateDir = "DukeScriptSwaggerCodegen";

        apiPackage = "io.swagger.client.api";

        modelPackage = "io.swagger.client.model";

        reservedWords = new HashSet<String>(
        );

        additionalProperties.put("apiVersion", apiVersion);

        languageSpecificPrimitives = new HashSet<String>(

        );
    }

    @Override
    public String escapeReservedWord(String name) {
        return "_" + name;  // add an underscore to the name
    }

    public String modelFileFolder() {
        return outputFolder + "/" + sourceFolder + "/" + modelPackage().replace('.', File.separatorChar);
    }

    @Override
    public String apiFileFolder() {
        return outputFolder + "/" + sourceFolder + "/" + apiPackage().replace('.', File.separatorChar);
    }

}

最后是codegen-maven-plugin的配置:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dukescript.demo</groupId>
    <artifactId>swagger-codegen-test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <swagger-annotations-version>1.5.21</swagger-annotations-version>
        <jersey-version>2.25.1</jersey-version>
        <jackson-version>2.9.5</jackson-version>
        <jodatime-version>2.7</jodatime-version>
        <maven-plugin-version>1.0.0</maven-plugin-version>
        <junit-version>4.8.1</junit-version>
        <default.package>com.dukescripd.demo</default.package>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <version>2.3.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>src/main/resources/petstore.json</inputSpec>
                            <language>com.dukescript.swagger.codegen.DukescriptswaggercodegenGenerator</language>
                            <output>${project.build.directory}/generated-sources</output>
                            <apiPackage>${default.package}.handler</apiPackage>
                            <modelPackage>${default.package}.model</modelPackage>
                            <invokerPackage>${default.package}.handler</invokerPackage> 
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>io.swagger</groupId>
                        <artifactId>DukeScriptSwaggerCodegen-swagger-codegen</artifactId>
                        <version>1.0.0</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>${swagger-annotations-version}</version>
        </dependency>
    </dependencies>
</project>

1 个答案:

答案 0 :(得分:1)

在您的模型中,添加

{{#models}}
{{#model}}

   ...

{{/model}}
{{/models}}

包含{{#vars}}的代码段。

Example