From ed211050f1c9d9c45456c006ba1ded30271d1d91 Mon Sep 17 00:00:00 2001 From: Qluxzz Date: Tue, 26 Nov 2024 10:30:04 +0100 Subject: [PATCH] [Elm] Fix not sanitizing param name (#20171) * Add failing example * Sanitize param name * Regenerate fixed sample * Override toParamName instead --- .../codegen/languages/ElmClientCodegen.java | 15 +++++++++++- .../resources/elm/formParamEncoder.mustache | 2 +- .../src/main/resources/elm/operation.mustache | 2 +- .../src/test/resources/3_0/elm.yaml | 23 +++++++++++++++++++ .../client/elm/src/Api/Request/Default.elm | 13 +++++++++++ 5 files changed, 52 insertions(+), 3 deletions(-) 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 3bc81accdc3a..6e9dc7d632ea 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 @@ -228,10 +228,23 @@ public String toEnumName(CodegenProperty property) { @Override public String toVarName(String name) { - final String varName = camelize(name.replaceAll("[^a-zA-Z0-9_]", ""), LOWERCASE_FIRST_LETTER); + // Replace space with _ (underscore) so camelize works as expected + final String varName = camelize(name.replaceAll(" ", "_").replaceAll("[^a-zA-Z0-9_]", ""), + LOWERCASE_FIRST_LETTER); return isReservedWord(varName) ? escapeReservedWord(name) : varName; } + @Override + public String toParamName(String name) { + // obtain the name from parameterNameMapping directly if provided + if (parameterNameMapping.containsKey(name)) { + return parameterNameMapping.get(name); + } + + // params should be lowerCamelCase + return toVarName(name); + } + @Override public String toEnumVarName(String value, String datatype) { String camelized = camelize(value.replace(" ", "_").replace("(", "_").replace(")", "")); // TODO FIXME escape properly diff --git a/modules/openapi-generator/src/main/resources/elm/formParamEncoder.mustache b/modules/openapi-generator/src/main/resources/elm/formParamEncoder.mustache index 7198a2e7ae4b..e3ad4ee82463 100644 --- a/modules/openapi-generator/src/main/resources/elm/formParamEncoder.mustache +++ b/modules/openapi-generator/src/main/resources/elm/formParamEncoder.mustache @@ -1 +1 @@ -{{^required}}Maybe.map ({{/required}}{{#required}}Just <| {{/required}}{{^isFile}}Http.stringPart {{/isFile}}{{#isFile}}Http.filePart {{/isFile}}"{{paramName}}"{{#isBoolean}}(\val -> if val then "true" else "false"){{/isBoolean}}{{#isDateTime}}Api.Time.dateTimeToString{{/isDateTime}}{{#isDate}}Api.Time.dateToString{{/isDate}}{{#isInteger}}String.fromInt{{/isInteger}}{{#isLong}}String.fromInt{{/isLong}}{{#isNumber}}String.fromFloat{{/isNumber}}{{#isFloat}}String.fromFloat{{/isFloat}}{{#isDouble}}String.fromFloat{{/isDouble}}{{#allowableValues}}Api.Data.stringFrom{{#datatypeWithEnum}}{{datatypeWithEnum}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{dataType}}{{/datatypeWithEnum}}{{/allowableValues}}{{#isUuid}}Uuid.toString{{/isUuid}}{{^required}}){{/required}} {{paramName}} \ No newline at end of file +{{^required}}Maybe.map ({{/required}}{{#required}}Just <| {{/required}}{{^isFile}}Http.stringPart {{/isFile}}{{#isFile}}Http.filePart {{/isFile}}"{{baseName}}"{{#isBoolean}}(\val -> if val then "true" else "false"){{/isBoolean}}{{#isDateTime}}Api.Time.dateTimeToString{{/isDateTime}}{{#isDate}}Api.Time.dateToString{{/isDate}}{{#isInteger}}String.fromInt{{/isInteger}}{{#isLong}}String.fromInt{{/isLong}}{{#isNumber}}String.fromFloat{{/isNumber}}{{#isFloat}}String.fromFloat{{/isFloat}}{{#isDouble}}String.fromFloat{{/isDouble}}{{#allowableValues}}Api.Data.stringFrom{{#datatypeWithEnum}}{{datatypeWithEnum}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{dataType}}{{/datatypeWithEnum}}{{/allowableValues}}{{#isUuid}}Uuid.toString{{/isUuid}}{{^required}}){{/required}} {{paramName}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/elm/operation.mustache b/modules/openapi-generator/src/main/resources/elm/operation.mustache index 6e9f4282deca..d7c5db32413a 100644 --- a/modules/openapi-generator/src/main/resources/elm/operation.mustache +++ b/modules/openapi-generator/src/main/resources/elm/operation.mustache @@ -56,7 +56,7 @@ import File exposing (File){{/includeFile}} Api.request "{{httpMethod}}" "{{path}}" - [{{#pathParams}} ( "{{paramName}}", {{>paramToString}} {{>paramName}} ){{#-last}} {{/-last}}{{^-last}},{{/-last}}{{/pathParams}}] + [{{#pathParams}} ( "{{baseName}}", {{>paramToString}} {{>paramName}} ){{#-last}} {{/-last}}{{^-last}},{{/-last}}{{/pathParams}}] [{{#queryParams}} ( "{{baseName}}", {{#required}}Just <| {{/required}}{{^required}}Maybe.map {{/required}}{{>paramToString}} {{>paramName}} ){{#-last}} {{/-last}}{{^-last}},{{/-last}}{{/queryParams}}] [{{#headerParams}} ( "{{baseName}}", {{#required}}Just <| {{/required}}{{^required}}Maybe.map {{/required}}{{>paramToString}} {{>paramName}} ){{#-last}} {{/-last}}{{^-last}},{{/-last}}{{/headerParams}}] {{#bodyParam}} diff --git a/modules/openapi-generator/src/test/resources/3_0/elm.yaml b/modules/openapi-generator/src/test/resources/3_0/elm.yaml index 162d9b976127..03ac359d2483 100644 --- a/modules/openapi-generator/src/test/resources/3_0/elm.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/elm.yaml @@ -126,6 +126,29 @@ paths: schema: type: string format: uuid + /param-sanitize/{test id}: + post: + responses: + '405': + description: Invalid input + parameters: + - name: "query with spaces" + in: query + schema: + type: string + - name: "test id" + in: path + required: true + schema: + type: string + requestBody: + content: + application/x-www-form-urlencoded: + schema: + type: object + properties: + "name with spaces": + type: string components: schemas: Absent: diff --git a/samples/openapi3/client/elm/src/Api/Request/Default.elm b/samples/openapi3/client/elm/src/Api/Request/Default.elm index 241a142aad31..64fa28f4f1bf 100644 --- a/samples/openapi3/client/elm/src/Api/Request/Default.elm +++ b/samples/openapi3/client/elm/src/Api/Request/Default.elm @@ -16,6 +16,7 @@ module Api.Request.Default exposing ( headerPost, HeaderType(..), headerTypeVariants , maybeGet + , paramSanitizeTestIdPost , pathStringIntegerEnumerationGet, Enumeration(..), enumerationVariants , queryGet, Enum(..), enumVariants , securedPost @@ -134,6 +135,18 @@ maybeGet = Api.Data.maybeDecoder +paramSanitizeTestIdPost : String -> Maybe String -> Maybe String -> Api.Request () +paramSanitizeTestIdPost testId_path queryWithSpaces_query nameWithSpaces = + Api.request + "POST" + "/param-sanitize/{test id}" + [ ( "test id", identity testId_path ) ] + [ ( "query with spaces", Maybe.map identity queryWithSpaces_query ) ] + [] + (Just <| Http.multipartBody <| List.filterMap identity [ Maybe.map (Http.stringPart "name with spaces") nameWithSpaces ]) + (Json.Decode.succeed ()) + + pathStringIntegerEnumerationGet : String -> Int -> Enumeration -> Api.Request () pathStringIntegerEnumerationGet string_path integer_path enumeration_path = Api.request