在小胡子模板中访问 Swagger codegen 模型变量
Access Swagger codegen model vars in mustache template
我正在尝试使用 swagger codegen(版本 2.3.1)创建我的第一个自定义代码生成器。我的问题是,我似乎无法访问模型的 "vars"。我使用 petstore.json 示例作为输入,并使用 swagger-codegen-maven-plugin 来创建生成的源。
在我的小胡子模板中,我正在尝试访问 model properties。这适用于 "className" 或 "package" 等属性,但我无法访问列表类型 属性 "vars"。当 运行“-DdebugModels”时,显示变量并且 "hasVars" 被报告为 true。但是在生成的代码中没有使用 vars 并且 "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 的模板。此模板使用 "operations" 列表类型 属性,效果很好。
这是我的代码生成器:
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>
在你的model.mustache中添加
{{#models}}
{{#model}}
...
{{/model}}
{{/models}}
代码段周围 {{#vars}}
。
我正在尝试使用 swagger codegen(版本 2.3.1)创建我的第一个自定义代码生成器。我的问题是,我似乎无法访问模型的 "vars"。我使用 petstore.json 示例作为输入,并使用 swagger-codegen-maven-plugin 来创建生成的源。
在我的小胡子模板中,我正在尝试访问 model properties。这适用于 "className" 或 "package" 等属性,但我无法访问列表类型 属性 "vars"。当 运行“-DdebugModels”时,显示变量并且 "hasVars" 被报告为 true。但是在生成的代码中没有使用 vars 并且 "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 的模板。此模板使用 "operations" 列表类型 属性,效果很好。
这是我的代码生成器:
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>
在你的model.mustache中添加
{{#models}}
{{#model}}
...
{{/model}}
{{/models}}
代码段周围 {{#vars}}
。