diff --git a/modules/swagger-codegen/pom.xml b/modules/swagger-codegen/pom.xml index 05206eb2f9b..0cdf01a7f68 100644 --- a/modules/swagger-codegen/pom.xml +++ b/modules/swagger-codegen/pom.xml @@ -219,6 +219,11 @@ jmustache ${jmustache-version} + + com.github.jknack + handlebars + ${handlebars-version} + commons-io commons-io diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java index 7f89ef738a0..fbeb8e956fa 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java @@ -11,6 +11,7 @@ public class CodegenModel { + public String parent, parentSchema; public List interfaces; @@ -175,4 +176,199 @@ public int hashCode() { result = 31 * result + Objects.hash(parentVars); return result; } + + //---- Properties + + public String getParent() { return parent; } + public void setParent(String parent) { + this.parent = parent; + } + + public String getParentSchema() { return parentSchema; } + public void setParentSchema(String parentSchema) { + this.parentSchema = parentSchema; + } + + public List getInterfaces() { return interfaces; } + public void setInterfaces(List interfaces) { + this.interfaces = interfaces; + } + + public CodegenModel getParentModel() { return parentModel; } + public void setParentModel(CodegenModel parentModel) { + this.parentModel = parentModel; + } + + public List getInterfaceModels() { return interfaceModels; } + public void setInterfaceModels(List interfaceModels) { + this.interfaceModels = interfaceModels; + } + + public List getChildren() { return children; } + public void setChildren(List children) { + this.children = children; + } + + public String getName() { return name; } + public void setName(String name) { + this.name = name; + } + + public String getClassname() { return classname; } + public void setClassname(String classname) { + this.classname = classname; + } + + public String getTitle() { return title; } + public void setTitle(String title) { + this.title = title; + } + + public String getDescription() { return description; } + public void setDescription(String description) { + this.description = description; + } + + public String getClassVarName() { return classVarName; } + public void setClassVarName(String classVarName) { + this.classVarName = classVarName; + } + + public String getModelJson() { return modelJson; } + public void setModelJson(String modelJson) { + this.modelJson = modelJson; + } + + public String getDataType() { return dataType; } + public void setDataType(String dataType) { + this.dataType = dataType; + } + + public String getClassFilename() { return classFilename; } + public void setClassFilename(String classFilename) { + this.classFilename = classFilename; + } + + public String getUnescapedDescription() { return unescapedDescription; } + public void setUnescapedDescription(String unescapedDescription) { + this.unescapedDescription = unescapedDescription; + } + + public String getDiscriminator() { return discriminator; } + public void setDiscriminator(String discriminator) { + this.discriminator = discriminator; + } + + public String getDefaultValue() { return defaultValue; } + public void setDefaultValue(String defaultValue) { + this.defaultValue = defaultValue; + } + + public String getArrayModelType() { return arrayModelType; } + public void setArrayModelType(String arrayModelType) { + this.arrayModelType = arrayModelType; + } + + public List getVars() { return vars; } + public void setVars(List vars) { + this.vars = vars; + } + + public List getRequiredVars() { return requiredVars; } + public void setRequiredVars(List requiredVars) { + this.requiredVars = requiredVars; + } + + public List getOptionalVars() { return optionalVars; } + public void setOptionalVars(List optionalVars) { + this.optionalVars = optionalVars; + } + + public List getReadOnlyVars() { return readOnlyVars; } + public void setReadOnlyVars(List readOnlyVars) { + this.readOnlyVars = readOnlyVars; + } + + public List getReadWriteVars() { return readWriteVars; } + public void setReadWriteVars(List readWriteVars) { this.readWriteVars = readWriteVars; + } + + public List getAllVars() { return allVars; } + public void setAllVars(List allVars) { this.allVars = allVars; } + + public List getParentVars() { return parentVars; } + public void setParentVars(List parentVars) { this.parentVars = parentVars; } + + public Map getAllowableValues() { return allowableValues; } + public void setAllowableValues(Map allowableValues) { this.allowableValues = allowableValues; } + + public Set getMandatory() { return mandatory; } + public void setMandatory(Set mandatory) { this.mandatory = mandatory; } + + public Set getAllMandatory() { return allMandatory; } + public void setAllMandatory(Set allMandatory) { this.allMandatory = allMandatory; } + + public Set getImports() { return imports; } + public void setImports(Set imports) { this.imports = imports; } + + public boolean isHasVars() { return hasVars; } + public void setHasVars(boolean hasVars) { this.hasVars = hasVars; } + + public boolean isEmptyVars() { return emptyVars; } + public void setEmptyVars(boolean emptyVars) { this.emptyVars = emptyVars; } + + public boolean isHasMoreModels() { return hasMoreModels; } + public void setHasMoreModels(boolean hasMoreModels) { this.hasMoreModels = hasMoreModels; } + + public boolean isHasEnums() { return hasEnums; } + public void setHasEnums(boolean hasEnums) { this.hasEnums = hasEnums; } + + public boolean isEnum() { return isEnum; } + public void setEnum(boolean anEnum) { isEnum = anEnum; } + + public boolean isHasRequired() { return hasRequired; } + public void setHasRequired(boolean hasRequired) { this.hasRequired = hasRequired; } + + public boolean isArrayModel() { return isArrayModel; } + public void setArrayModel(boolean arrayModel) { isArrayModel = arrayModel; } + + public boolean isHasChildren() { + return hasChildren; + } + + public void setHasChildren(boolean hasChildren) { + this.hasChildren = hasChildren; + } + + public boolean isHasOnlyReadOnly() { + return hasOnlyReadOnly; + } + + public void setHasOnlyReadOnly(boolean hasOnlyReadOnly) { + this.hasOnlyReadOnly = hasOnlyReadOnly; + } + + public ExternalDocs getExternalDocs() { + return externalDocs; + } + + public void setExternalDocs(ExternalDocs externalDocs) { + this.externalDocs = externalDocs; + } + + public Map getVendorExtensions() { + return vendorExtensions; + } + + public void setVendorExtensions(Map vendorExtensions) { + this.vendorExtensions = vendorExtensions; + } + + public String getAdditionalPropertiesType() { + return additionalPropertiesType; + } + + public void setAdditionalPropertiesType(String additionalPropertiesType) { + this.additionalPropertiesType = additionalPropertiesType; + } } diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenProperty.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenProperty.java index b5e383a617b..7ab06b0513d 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenProperty.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenProperty.java @@ -325,5 +325,295 @@ public CodegenProperty clone() { } } + //---- Properties + public String getBaseName() { return baseName; } + public void setBaseName(String baseName) { + this.baseName = baseName; + } + + public String getComplexType() { return complexType; } + public void setComplexType(String complexType) { + this.complexType = complexType; + } + + public String getGetter() { return getter; } + public void setGetter(String getter) { + this.getter = getter; + } + + public String getSetter() { return setter; } + public void setSetter(String setter) { + this.setter = setter; + } + + public String getDescription() { return description; } + public void setDescription(String description) { + this.description = description; + } + + public String getDatatype() { return datatype; } + public void setDatatype(String datatype) { + this.datatype = datatype; + } + + public String getDatatypeWithEnum() { return datatypeWithEnum; } + public void setDatatypeWithEnum(String datatypeWithEnum) { + this.datatypeWithEnum = datatypeWithEnum; + } + + public String getDataFormat() { return dataFormat; } + public void setDataFormat(String dataFormat) { + this.dataFormat = dataFormat; + } + + public String getName() { return name; } + public void setName(String name) { + this.name = name; + } + + public String getMin() { return min; } + public void setMin(String min) { + this.min = min; + } + + public String getMax() { return max; } + public void setMax(String max) { + this.max = max; + } + + public String getDefaultValue() { return defaultValue; } + public void setDefaultValue(String defaultValue) { + this.defaultValue = defaultValue; + } + + public String getDefaultValueWithParam() { return defaultValueWithParam; } + public void setDefaultValueWithParam(String defaultValueWithParam) { + this.defaultValueWithParam = defaultValueWithParam; + } + + public String getBaseType() { return baseType; } + public void setBaseType(String baseType) { + this.baseType = baseType; + } + + public String getContainerType() { return containerType; } + public void setContainerType(String containerType) { + this.containerType = containerType; + } + + public String getTitle() { return title; } + public void setTitle(String title) { + this.title = title; + } + + public String getUnescapedDescription() { return unescapedDescription; } + public void setUnescapedDescription(String unescapedDescription) { + this.unescapedDescription = unescapedDescription; + } + + public Integer getMaxLength() { return maxLength; } + public void setMaxLength(Integer maxLength) { + this.maxLength = maxLength; + } + + public Integer getMinLength() { return minLength; } + public void setMinLength(Integer minLength) { + this.minLength = minLength; + } + + public String getPattern() { return pattern; } + public void setPattern(String pattern) { + this.pattern = pattern; + } + + public String getExample() { return example; } + public void setExample(String example) { + this.example = example; + } + + public String getJsonSchema() { return jsonSchema; } + public void setJsonSchema(String jsonSchema) { + this.jsonSchema = jsonSchema; + } + + public String getMinimum() { return minimum; } + public void setMinimum(String minimum) { + this.minimum = minimum; + } + + public String getMaximum() { return maximum; } + public void setMaximum(String maximum) { + this.maximum = maximum; + } + + public boolean isExclusiveMinimum() { return exclusiveMinimum; } + public void setExclusiveMinimum(boolean exclusiveMinimum) { + this.exclusiveMinimum = exclusiveMinimum; + } + + public boolean isExclusiveMaximum() { return exclusiveMaximum; } + public void setExclusiveMaximum(boolean exclusiveMaximum) { + this.exclusiveMaximum = exclusiveMaximum; + } + + public boolean isHasMore() { return hasMore; } + public void setHasMore(boolean hasMore) { + this.hasMore = hasMore; + } + + public boolean isRequired() { return required; } + public void setRequired(boolean required) { + this.required = required; + } + + public boolean isSecondaryParam() { return secondaryParam; } + public void setSecondaryParam(boolean secondaryParam) { + this.secondaryParam = secondaryParam; + } + + public boolean isHasMoreNonReadOnly() { return hasMoreNonReadOnly; } + public void setHasMoreNonReadOnly(boolean hasMoreNonReadOnly) { + this.hasMoreNonReadOnly = hasMoreNonReadOnly; + } + + public boolean isPrimitiveType() { return isPrimitiveType; } + public void setPrimitiveType(boolean primitiveType) { + isPrimitiveType = primitiveType; + } + + public boolean isContainer() { return isContainer; } + public void setContainer(boolean container) { + isContainer = container; + } + + public boolean isNotContainer() { return isNotContainer; } + public void setNotContainer(boolean notContainer) { + isNotContainer = notContainer; + } + + public boolean isString() { return isString; } + public void setString(boolean string) { + isString = string; + } + + public boolean isInteger() { return isInteger; } + public void setInteger(boolean integer) { + isInteger = integer; + } + + public boolean isLong() { return isLong; } + public void setLong(boolean aLong) { + isLong = aLong; + } + + public boolean isFloat() { return isFloat; } + public void setFloat(boolean aFloat) { + isFloat = aFloat; + } + + public boolean isDouble() { return isDouble; } + public void setDouble(boolean aDouble) { + isDouble = aDouble; + } + + public boolean isByteArray() { return isByteArray; } + public void setByteArray(boolean byteArray) { + isByteArray = byteArray; + } + + public boolean isBinary() { return isBinary; } + public void setBinary(boolean binary) { + isBinary = binary; + } + + public boolean isFile() { return isFile; } + public void setFile(boolean file) { + isFile = file; + } + + public boolean isBoolean() { return isBoolean; } + public void setBoolean(boolean aBoolean) { + isBoolean = aBoolean; + } + + public boolean isDate() { return isDate; } + public void setDate(boolean date) { + isDate = date; + } + + public boolean isDateTime() { return isDateTime; } + public void setDateTime(boolean dateTime) { + isDateTime = dateTime; + } + + public boolean isListContainer() { return isListContainer; } + public void setListContainer(boolean listContainer) { + isListContainer = listContainer; + } + + public boolean isMapContainer() { return isMapContainer; } + public void setMapContainer(boolean mapContainer) { + isMapContainer = mapContainer; + } + + public Boolean isEnum() { return isEnum; } + public void setEnum(boolean anEnum) { + isEnum = anEnum; + } + + public boolean isReadOnly() { return isReadOnly; } + public void setReadOnly(boolean readOnly) { + isReadOnly = readOnly; + } + + public List get_enum() { return _enum; } + public void set_enum(List _enum) { + this._enum = _enum; + } + + public Map getAllowableValues() { return allowableValues; } + public void setAllowableValues(Map allowableValues) { + this.allowableValues = allowableValues; + } + + public CodegenProperty getItems() { return items; } + public void setItems(CodegenProperty items) { + this.items = items; + } + + public Map getVendorExtensions() { return vendorExtensions; } + public void setVendorExtensions(Map vendorExtensions) { + this.vendorExtensions = vendorExtensions; + } + + public boolean isHasValidation() { return hasValidation; } + public void setHasValidation(boolean hasValidation) { + this.hasValidation = hasValidation; + } + + public boolean isInherited() { return isInherited; } + public void setInherited(boolean inherited) { + isInherited = inherited; + } + + public String getNameInCamelCase() { return nameInCamelCase; } + public void setNameInCamelCase(String nameInCamelCase) { + this.nameInCamelCase = nameInCamelCase; + } + + public String getEnumName() { return enumName; } + public void setEnumName(String enumName) { + this.enumName = enumName; + } + + public Integer getMaxItems() { return maxItems; } + public void setMaxItems(Integer maxItems) { + this.maxItems = maxItems; + } + + public Integer getMinItems() { return minItems; } + public void setMinItems(Integer minItems) { + this.minItems = minItems; + } } diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java index 6bd8931fc61..06c5f5b9356 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java @@ -20,6 +20,9 @@ import org.apache.commons.lang3.StringUtils; public class DefaultGenerator extends AbstractGenerator implements Generator { + + private static final String ENV_NAME_HANDLEBARS = "handlebars"; + protected final Logger LOGGER = LoggerFactory.getLogger(DefaultGenerator.class); protected CodegenConfig config; protected ClientOptInput opts; @@ -31,6 +34,8 @@ public class DefaultGenerator extends AbstractGenerator implements Generator { private Boolean generateApiTests = null; private Boolean generateApiDocumentation = null; private Boolean generateModelTests = null; + private boolean useHandlebars = false; + private HandlebarsTemplateRenderer handlebarsTemplateRenderer = null; private Boolean generateModelDocumentation = null; private String basePath; private String basePathWithoutHost; @@ -137,6 +142,10 @@ private void configureGeneratorProperties() { basePath = config.escapeText(getHost()); basePathWithoutHost = config.escapeText(swagger.getBasePath()); + if (System.getProperty(ENV_NAME_HANDLEBARS) != null) { + useHandlebars = true; + handlebarsTemplateRenderer = new HandlebarsTemplateRenderer(config); + } } private void configureSwaggerInfo() { @@ -675,11 +684,15 @@ public List generate() { private File processTemplateToFile(Map templateData, String templateName, String outputFilename) throws IOException { String adjustedOutputFilename = outputFilename.replaceAll("//", "/").replace('/', File.separatorChar); if(ignoreProcessor.allowsFile(new File(adjustedOutputFilename))) { - String templateFile = getFullTemplateFile(config, templateName); - String template = readTemplate(templateFile); - Mustache.Compiler compiler = Mustache.compiler(); - compiler = config.processCompiler(compiler); - Template tmpl = compiler + String renderedTemplate; + if (useHandlebars) { + renderedTemplate = handlebarsTemplateRenderer.renderHandlebarTemplate(templateName, templateData); + } else { + String templateFile = getFullTemplateFile(config, templateName); + String template = readTemplate(templateFile); + Mustache.Compiler compiler = Mustache.compiler(); + compiler = config.processCompiler(compiler); + Template tmpl = compiler .withLoader(new Mustache.TemplateLoader() { @Override public Reader getTemplate(String name) { @@ -688,8 +701,9 @@ public Reader getTemplate(String name) { }) .defaultValue("") .compile(template); - - writeToFile(adjustedOutputFilename, tmpl.execute(templateData)); + renderedTemplate = tmpl.execute(templateData); + } + writeToFile(adjustedOutputFilename, renderedTemplate); return new File(adjustedOutputFilename); } diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/HandlebarsTemplateRenderer.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/HandlebarsTemplateRenderer.java new file mode 100644 index 00000000000..07b06ab1dd5 --- /dev/null +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/HandlebarsTemplateRenderer.java @@ -0,0 +1,55 @@ +package io.swagger.codegen; + +import static com.google.common.base.Objects.equal; + +import java.io.IOException; +import java.util.Map; + +import com.github.jknack.handlebars.Handlebars; +import com.github.jknack.handlebars.Helper; +import com.github.jknack.handlebars.Options; +import com.github.jknack.handlebars.io.FileTemplateLoader; +import com.github.jknack.handlebars.io.TemplateLoader; + +/** + * @author Tobias Gmünder + */ +public class HandlebarsTemplateRenderer { + + private final CodegenConfig config; + + private final Handlebars handlebars; + + /** + * Creates a new instance with the given {@code config}. + * + * @param config the {@link CodegenConfig}. + */ + public HandlebarsTemplateRenderer(CodegenConfig config) { + this.config = config; + final TemplateLoader templateLoader = new FileTemplateLoader(config.templateDir()); + templateLoader.setSuffix(".mustache"); + this.handlebars = new Handlebars(templateLoader); + + handlebars.registerHelper("equals", new Helper() { + @Override + public Object apply(Object context, Options options) throws IOException { + final Object obj = options.param(0); + return equal(context, obj) ? options.fn() : options.inverse(); + } + }); + } + + public String renderHandlebarTemplate(String templateName, Map templateData) throws IOException { + final com.github.jknack.handlebars.Template hTemplate = handlebars.compile(templateName.replaceFirst(".mustache", "")); + return hTemplate.apply(templateData); + } + + /** + * @return the {@link Handlebars} instance used for template rendering. Use this method to register custom helpers. + */ + public Handlebars getHandlebars() { + return this.handlebars; + } + +} diff --git a/pom.xml b/pom.xml index 92c8bf18c8e..c327a803bf5 100644 --- a/pom.xml +++ b/pom.xml @@ -914,5 +914,6 @@ 2.19.1 1.25 0.9.10 + 4.0.6