Skip to content

Commit

Permalink
[Elm] Fix not sanitizing param name (#20171)
Browse files Browse the repository at this point in the history
* Add failing example

* Sanitize param name

* Regenerate fixed sample

* Override toParamName instead
  • Loading branch information
Qluxzz authored Nov 26, 2024
1 parent 55f02e9 commit ed21105
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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}}
{{^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}}
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
23 changes: 23 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/elm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions samples/openapi3/client/elm/src/Api/Request/Default.elm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
module Api.Request.Default exposing
( headerPost, HeaderType(..), headerTypeVariants
, maybeGet
, paramSanitizeTestIdPost
, pathStringIntegerEnumerationGet, Enumeration(..), enumerationVariants
, queryGet, Enum(..), enumVariants
, securedPost
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ed21105

Please sign in to comment.