Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
[scala-akka-http] Added a config option akkaHttpVersion
It's set in the generated build.sbt.
  • Loading branch information
Olivier Leonard committed Mar 31, 2020
commit de57a5d6aaf335f3cce75712691ea29262ed8437
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import java.io.File;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openapitools.codegen.meta.features.*;
import org.slf4j.Logger;
Expand All @@ -18,6 +20,11 @@ public class ScalaAkkaHttpServerCodegen extends AbstractScalaCodegen implements
protected String artifactVersion;
protected String invokerPackage;

protected String akkaHttpVersion;

public static final String AKKA_HTTP_VERSION = "akkaHttpVersion";
public static final String AKKA_HTTP_VERSION_DESC = "The version of akka-http";

static Logger LOGGER = LoggerFactory.getLogger(ScalaAkkaHttpServerCodegen.class);

public CodegenType getTag() {
Expand Down Expand Up @@ -68,6 +75,7 @@ public ScalaAkkaHttpServerCodegen() {
apiPackage = "org.openapitools.server.api";
modelPackage = "org.openapitools.server.model";
invokerPackage = "org.openapitools.server";
akkaHttpVersion = "10.1.9";

setReservedWordsLowerCase(
Arrays.asList(
Expand All @@ -82,6 +90,7 @@ public ScalaAkkaHttpServerCodegen() {
cliOptions.add(CliOption.newString(CodegenConstants.GROUP_ID, CodegenConstants.GROUP_ID_DESC).defaultValue(groupId));
cliOptions.add(CliOption.newString(CodegenConstants.ARTIFACT_ID, CodegenConstants.ARTIFACT_ID).defaultValue(artifactId));
cliOptions.add(CliOption.newString(CodegenConstants.ARTIFACT_VERSION, CodegenConstants.ARTIFACT_VERSION_DESC).defaultValue(artifactVersion));
cliOptions.add(CliOption.newString(AKKA_HTTP_VERSION, AKKA_HTTP_VERSION_DESC).defaultValue(akkaHttpVersion));

importMapping.remove("Seq");
importMapping.remove("List");
Expand Down Expand Up @@ -110,7 +119,6 @@ public ScalaAkkaHttpServerCodegen() {
instantiationTypes.put("map", "Map");

supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("build.sbt.mustache", "", "build.sbt"));
}

@Override
Expand All @@ -128,17 +136,28 @@ public void processOpts() {
} else {
additionalProperties.put(CodegenConstants.GROUP_ID, groupId);
}

if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_ID)) {
artifactId = (String) additionalProperties.get(CodegenConstants.ARTIFACT_ID);
} else {
additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId);
}

if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_VERSION)) {
artifactVersion = (String) additionalProperties.get(CodegenConstants.ARTIFACT_VERSION);
} else {
additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion);
}

if (additionalProperties.containsKey(AKKA_HTTP_VERSION)) {
akkaHttpVersion = (String) additionalProperties.get(AKKA_HTTP_VERSION);
} else {
additionalProperties.put(AKKA_HTTP_VERSION, akkaHttpVersion);
}

parseAkkaHttpVersion();

supportingFiles.add(new SupportingFile("build.sbt.mustache", "", "build.sbt"));
supportingFiles.add(new SupportingFile("controller.mustache",
(sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "Controller.scala"));
supportingFiles.add(new SupportingFile("helper.mustache",
Expand All @@ -149,6 +168,38 @@ public void processOpts() {
(sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "MultipartDirectives.scala"));
}

private static final String IS_10_1_9_PLUS = "akkaHttp10_1_9_plus";
private boolean is10_1_9AndAbove = false;

private static final Pattern akkaVersionPattern = Pattern.compile("([0-9]+)(\\.([0-9]+))?(\\.([0-9]+))?");
private void parseAkkaHttpVersion() {
Matcher matcher = akkaVersionPattern.matcher(akkaHttpVersion);
if (matcher.matches()) {
String majorS = matcher.group(1);
String minorS = matcher.group(3);
String patchS = matcher.group(5);
int major = 0, minor = 0, patch = 0;
try {
major = Integer.parseInt(majorS);
minor = Integer.parseInt(minorS);
patch = Integer.parseInt(patchS);
} catch (NumberFormatException e) {
LOGGER.warn("Unable to parse " + AKKA_HTTP_VERSION + ": " + akkaHttpVersion);
}
if (major > 10) {
is10_1_9AndAbove = true;
} else if (major == 10) {
if (minor > 1) {
is10_1_9AndAbove = true;
} else if (patch >= 9) {
is10_1_9AndAbove = true;
}
}
}

additionalProperties.put(IS_10_1_9_PLUS, is10_1_9AndAbove);
}

@Override
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, List<Server> servers) {
CodegenOperation codegenOperation = super.fromOperation(path, httpMethod, operation, servers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ scalaVersion := "2.12.8"

libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-stream" % "2.5.21",
"com.typesafe.akka" %% "akka-http" % "10.1.7"
"com.typesafe.akka" %% "akka-http" % "{{akkaHttpVersion}}"
)
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ object StringDirectives extends StringDirectives {

protected def handleFieldResult[T](fieldName: String, result: Future[T]): Directive1[T] = onComplete(result).flatMap {
case Success(x) => provide(x)
case Failure(Unmarshaller.NoContentException) => reject(MissingFormFieldRejection(fieldName))
case Failure(x: UnsupportedContentTypeException) => reject(UnsupportedRequestContentTypeRejection(x.supported))
case Failure(Unmarshaller.NoContentException) => reject(MissingFormFieldRejection(fieldName)){{#akkaHttp10_1_9_plus}}
case Failure(x: UnsupportedContentTypeException) => reject(UnsupportedRequestContentTypeRejection(x.supported, x.actualContentType)){{/akkaHttp10_1_9_plus}}{{^akkaHttp10_1_9_plus}}
case Failure(x: UnsupportedContentTypeException) => reject(UnsupportedRequestContentTypeRejection(x.supported)){{/akkaHttp10_1_9_plus}}
case Failure(x) => reject(MalformedFormFieldRejection(fieldName, if (x.getMessage == null) "" else x.getMessage, Option(x.getCause)))
}

Expand Down