I'm trying to create my first custom codegenerator with swagger codegen (Version 2.3.1). My problem is, that I cannot seem to access the "vars" of a model. I'm using the petstore.json sample as input, and the swagger-codegen-maven-plugin for creating the generated sources.
In my mustache template I'm trying to access the model properties. This works for properties like "className" or "package", but I'm not able to access the list type property "vars". When running with "-DdebugModels", the vars are shown and also "hasVars" is reported as true. But in the generated code the vars are not used and "hasVars" seems to be false. I've simplified my template to show it here. First the template:
package {{package}};
{{classname}}
{{#vars}}
{{{datatype}}} {{name}} = {{{defaultValue}}}
{{/vars}}
{{#hasVars}}
hasVars
{{/hasVars}}
{{^hasVars}}
has No Vars
{{/hasVars}}
This is the output for Pet as an example:
package com.dukescripd.demo.model;
Pet
has No Vars
I've started my project with the stubs generated by codegen. This generates some mustache templates including one for apis. This template uses the "operations" list type property, which works fine.
Here's my 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);
}
}
And finally the configuration of the 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>
In your model.mustache, add
{{#models}}
{{#model}}
...
{{/model}}
{{/models}}
around the snippet with {{#vars}}
.