Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,23 @@
package org.openapitools.codegen.languages;

import org.openapitools.codegen.*;
import org.openapitools.codegen.templating.mustache.CamelCaseLambda;
import org.openapitools.codegen.templating.mustache.IndentedLambda;
import org.openapitools.codegen.templating.mustache.LowercaseLambda;
import org.openapitools.codegen.templating.mustache.TitlecaseLambda;
import org.openapitools.codegen.templating.mustache.UppercaseLambda;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableMap;
import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Template;
import io.swagger.v3.oas.models.Operation;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class OpenAPIYamlGenerator extends DefaultCodegen implements CodegenConfig {
Expand Down Expand Up @@ -61,6 +75,44 @@ public void processOpts() {
}
LOGGER.info("Output file [outputFile={}]", outputFile);
supportingFiles.add(new SupportingFile("openapi.mustache", outputFile));

addMustacheLambdas(additionalProperties);
}

private void addMustacheLambdas(Map<String, Object> objs) {

Map<String, Mustache.Lambda> lambdas = new ImmutableMap.Builder<String, Mustache.Lambda>()
.put("lowercase", new LowercaseLambda().generator(this))
.put("uppercase", new UppercaseLambda())
.put("titlecase", new TitlecaseLambda())
.put("camelcase", new CamelCaseLambda().generator(this))
.put("indented", new IndentedLambda())
.put("indented_8", new IndentedLambda(8, " "))
.put("indented_12", new IndentedLambda(12, " "))
.put("indented_16", new IndentedLambda(16, " "))
.put("onchange", new OnChangeLambda())
.build();

if (objs.containsKey("lambda")) {
LOGGER.warn("A property named 'lambda' already exists. Mustache lambdas renamed from 'lambda' to '_lambda'. " +
"You'll likely need to use a custom template, " +
"see https://github.com/OpenAPITools/openapi-generator/blob/master/docs/templating.md. ");
objs.put("_lambda", lambdas);
} else {
objs.put("lambda", lambdas);
}
}

/**
* Group operations by resourcePath so that operations with same path and
* different http method can be rendered one after the other.
*/
@Override
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation
co, Map<String, List<CodegenOperation>> operations) {
List<CodegenOperation> opList = operations.computeIfAbsent(resourcePath,
k -> new ArrayList<>());
opList.add(co);
}

@Override
Expand All @@ -81,4 +133,24 @@ public String escapeUnsafeCharacters(String input) {
return input;
}

/**
* Lambda writes current fragment to the output when it is different than
* previous fragment.
*/
public static class OnChangeLambda implements Mustache.Lambda {
private static final Logger LOGGER = LoggerFactory.getLogger(OnChangeLambda.class);

private String lastVal = null;

@Override
public void execute(Template.Fragment frag, Writer out) throws IOException {
String curVal = frag.execute();
LOGGER.debug("[lastVal={}, curVal={}]", lastVal, curVal);
if (curVal != null && !curVal.equals(lastVal)) {
out.write(curVal);
lastVal = curVal;
}
}
}

}