Access Swagger codegen model vars in mustache template
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>
swagger swagger-codegen swagger-codegen-maven-plugin
add a comment |
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>
swagger swagger-codegen swagger-codegen-maven-plugin
add a comment |
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>
swagger swagger-codegen swagger-codegen-maven-plugin
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>
swagger swagger-codegen swagger-codegen-maven-plugin
swagger swagger-codegen swagger-codegen-maven-plugin
edited Nov 23 '18 at 12:53
monacotoni
asked Nov 23 '18 at 11:38
monacotonimonacotoni
487210
487210
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
In your model.mustache, add
{{#models}}
{{#model}}
...
{{/model}}
{{/models}}
around the snippet with {{#vars}}
.
Example
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445995%2faccess-swagger-codegen-model-vars-in-mustache-template%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In your model.mustache, add
{{#models}}
{{#model}}
...
{{/model}}
{{/models}}
around the snippet with {{#vars}}
.
Example
add a comment |
In your model.mustache, add
{{#models}}
{{#model}}
...
{{/model}}
{{/models}}
around the snippet with {{#vars}}
.
Example
add a comment |
In your model.mustache, add
{{#models}}
{{#model}}
...
{{/model}}
{{/models}}
around the snippet with {{#vars}}
.
Example
In your model.mustache, add
{{#models}}
{{#model}}
...
{{/model}}
{{/models}}
around the snippet with {{#vars}}
.
Example
answered Nov 23 '18 at 18:07
HelenHelen
33.7k476130
33.7k476130
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445995%2faccess-swagger-codegen-model-vars-in-mustache-template%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown