1. Operations are grouped together by resourcePath so that operation s with same resourcePath can be rendered one after the other. (#3202)

2. Lambdas registered: lowercase, uppercase, onchnage, etc.
This commit is contained in:
Michal Foksa 2019-07-12 18:41:26 +02:00 committed by William Cheng
parent 09b4bbc1e1
commit e81e650627

View File

@ -18,9 +18,23 @@
package org.openapitools.codegen.languages; package org.openapitools.codegen.languages;
import org.openapitools.codegen.*; 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.Logger;
import org.slf4j.LoggerFactory; 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; import java.util.Map;
public class OpenAPIYamlGenerator extends DefaultCodegen implements CodegenConfig { public class OpenAPIYamlGenerator extends DefaultCodegen implements CodegenConfig {
@ -61,6 +75,44 @@ public class OpenAPIYamlGenerator extends DefaultCodegen implements CodegenConfi
} }
LOGGER.info("Output file [outputFile={}]", outputFile); LOGGER.info("Output file [outputFile={}]", outputFile);
supportingFiles.add(new SupportingFile("openapi.mustache", 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 @Override
@ -81,4 +133,24 @@ public class OpenAPIYamlGenerator extends DefaultCodegen implements CodegenConfi
return 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;
}
}
}
} }