From d7d4803abdb858c174178acd824b6ee2bb4a1a4f Mon Sep 17 00:00:00 2001 From: Erik Timmers Date: Mon, 22 Oct 2018 22:17:32 +0200 Subject: [PATCH] [elm] Add flag to prefix custom type variants --- bin/openapi3/elm-petstore.sh | 2 +- .../codegen/languages/ElmClientCodegen.java | 35 +++++++++++++++++-- .../elm-0.18/.openapi-generator/VERSION | 2 +- .../petstore/elm/.openapi-generator/VERSION | 2 +- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/bin/openapi3/elm-petstore.sh b/bin/openapi3/elm-petstore.sh index 9090d50a87fb..da073a5ea5cd 100755 --- a/bin/openapi3/elm-petstore.sh +++ b/bin/openapi3/elm-petstore.sh @@ -27,6 +27,6 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -g elm -o samples/client/petstore/elm $@" +ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -g elm -t modules/openapi-generator/src/main/resources/elm -o samples/openapi3/client/petstore/elm --additional-properties elmPrefixCustomTypeVariants=true $@" java $JAVA_OPTS -jar $executable $ags diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ElmClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ElmClientCodegen.java index f710d426c2db..07d06a03b040 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ElmClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ElmClientCodegen.java @@ -59,8 +59,10 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig { private static final Logger LOGGER = LoggerFactory.getLogger(ElmClientCodegen.class); private Set customPrimitives = new HashSet(); private ElmVersion elmVersion = ElmVersion.ELM_019; + private Boolean elmPrefixCustomTypeVariants = false; private static final String ELM_VERSION = "elmVersion"; + private static final String ELM_PREFIX_CUSTOM_TYPE_VARIANTS = "elmPrefixCustomTypeVariants"; private static final String ENCODER = "elmEncoder"; private static final String DECODER = "elmDecoder"; private static final String DISCRIMINATOR_NAME = "discriminatorName"; @@ -162,13 +164,14 @@ public ElmClientCodegen() { supportedVersions.put("0.19", "Elm 0.19"); elmVersion.setEnum(supportedVersions); cliOptions.add(elmVersion); + final CliOption elmPrefixCustomTypeVariants = CliOption.newBoolean(ELM_PREFIX_CUSTOM_TYPE_VARIANTS, "Prefix custom type variants"); + cliOptions.add(elmPrefixCustomTypeVariants); } @Override public void processOpts() { super.processOpts(); - if (additionalProperties.containsKey(ELM_VERSION)) { final String version = (String) additionalProperties.get(ELM_VERSION); if ("0.18".equals(version)) { @@ -178,6 +181,10 @@ public void processOpts() { } } + if (additionalProperties.containsKey(ELM_PREFIX_CUSTOM_TYPE_VARIANTS)) { + elmPrefixCustomTypeVariants = Boolean.TRUE.equals(Boolean.valueOf(additionalProperties.get(ELM_PREFIX_CUSTOM_TYPE_VARIANTS).toString())); + } + if (StringUtils.isEmpty(System.getenv("ELM_POST_PROCESS_FILE"))) { if (elmVersion.equals(ElmVersion.ELM_018)) { // 0.18 LOGGER.info("Environment variable ELM_POST_PROCESS_FILE not defined so the Elm code may not be properly formatted. To define it, try `export ELM_POST_PROCESS_FILE=\"/usr/local/bin/elm-format --elm-version={} --yes\"` (Linux/Mac)", "0.18"); @@ -188,7 +195,7 @@ public void processOpts() { switch (elmVersion) { case ELM_018: - LOGGER.info("Elm version = 0.18"); + LOGGER.info("Elm version: 0.18"); additionalProperties.put("isElm018", true); supportingFiles.add(new SupportingFile("DateOnly018.mustache", "src", "DateOnly.elm")); supportingFiles.add(new SupportingFile("DateTime018.mustache", "src", "DateTime.elm")); @@ -196,7 +203,7 @@ public void processOpts() { supportingFiles.add(new SupportingFile("Main018.mustache", "src", "Main.elm")); break; case ELM_019: - LOGGER.info("Elm version = 0.19"); + LOGGER.info("Elm version: 0.19"); additionalProperties.put("isElm019", true); supportingFiles.add(new SupportingFile("DateOnly.mustache", "src", "DateOnly.elm")); supportingFiles.add(new SupportingFile("DateTime.mustache", "src", "DateTime.elm")); @@ -590,6 +597,28 @@ public void postProcessParameter(CodegenParameter parameter) { addEncoderAndDecoder(parameter.vendorExtensions, parameter.dataType, isPrimitiveType ? DataTypeExposure.PRIMITIVE : DataTypeExposure.EXTERNAL); } + @Override + public void updateCodegenPropertyEnum(final CodegenProperty property) { + super.updateCodegenPropertyEnum(property); + if (!elmPrefixCustomTypeVariants) { + return; + } + + final Map allowableValues = property.allowableValues; + if (allowableValues == null) { + return; + } + + final List> enumVars = (ArrayList>) allowableValues.get("enumVars"); + if (enumVars == null) { + return; + } + final String prefix = toEnumName(property); + for (Map enumVar : enumVars) { + enumVar.put("name", prefix + enumVar.get("name")); + } + } + private boolean isPrimitiveDataType(String dataType) { return languageSpecificPrimitives.contains(dataType); } diff --git a/samples/client/petstore/elm-0.18/.openapi-generator/VERSION b/samples/client/petstore/elm-0.18/.openapi-generator/VERSION index 6d94c9c2e12a..a65271290834 100644 --- a/samples/client/petstore/elm-0.18/.openapi-generator/VERSION +++ b/samples/client/petstore/elm-0.18/.openapi-generator/VERSION @@ -1 +1 @@ -3.3.0-SNAPSHOT \ No newline at end of file +3.3.2-SNAPSHOT \ No newline at end of file diff --git a/samples/client/petstore/elm/.openapi-generator/VERSION b/samples/client/petstore/elm/.openapi-generator/VERSION index 6d94c9c2e12a..a65271290834 100644 --- a/samples/client/petstore/elm/.openapi-generator/VERSION +++ b/samples/client/petstore/elm/.openapi-generator/VERSION @@ -1 +1 @@ -3.3.0-SNAPSHOT \ No newline at end of file +3.3.2-SNAPSHOT \ No newline at end of file