Bug Report Checklist
Description
The comment of DefaultCodegen.specialCharReplacements says that it uses the quotation mark (') as a prefix.
|
// How to encode special characters like $ |
|
// They are translated to words like "Dollar" and prefixed with ' |
|
// Then translated back during JSON encoding and decoding |
|
protected Map<String, String> specialCharReplacements = new LinkedHashMap<>(); |
But, neither the initializer of the table
|
protected void initializeSpecialCharacterMapping() { |
|
// Initialize special characters |
|
specialCharReplacements.put("$", "Dollar"); |
|
specialCharReplacements.put("^", "Caret"); |
nor its use-site DefaultCodegen.toVarName and its subroutine StringUtils.encode insert the quotation mark .
|
public String toVarName(final String name) { |
|
if (reservedWords.contains(name)) { |
|
return escapeReservedWord(name); |
|
} else if (name.chars().anyMatch(character -> specialCharReplacements.containsKey(String.valueOf((char) character)))) { |
|
return escape(name, specialCharReplacements, null, null); |
|
} |
|
return name; |
|
} |
|
public static String escape(final String name, final Map<String, String> replacementMap, |
|
final List<String> charactersToAllow, final String appendToReplacement) { |
|
EscapedNameOptions ns = new EscapedNameOptions(name, replacementMap.keySet(), charactersToAllow, appendToReplacement); |
|
return escapedWordsCache.get(ns, wordToEscape -> { |
|
String result = name.chars().mapToObj(c -> { |
|
String character = String.valueOf((char) c); |
|
if (charactersToAllow != null && charactersToAllow.contains(character)) { |
|
return character; |
|
} else if (replacementMap.containsKey(character)) { |
|
return replacementMap.get(character) + (appendToReplacement != null ? appendToReplacement: ""); |
|
} else { |
|
return character; |
|
} |
|
}).reduce( (c1, c2) -> c1 + c2).orElse(null); |
|
|
|
if (result != null) return result; |
|
throw new RuntimeException("Word '" + name + "' could not be escaped."); |
|
}); |
|
} |
Background: I noticed the problem when I was debugging HaskellServantCodegen. HaskellServantCodegen generates code that assumes the quotation mark prefixes, but the generated field names do not have the quotation mark prefixes.
openapi-generator version
OpenAPI declaration file content or url
Generation Details
Steps to reproduce
Related issues/PRs
Suggest a fix
I guess that the comment is outdated and the current behavior (not prefixed with the quotation mark) is intended behavior.
If so, I suggest simply removing and prefixed with ' from the comment.
Bug Report Checklist
Description
The comment of
DefaultCodegen.specialCharReplacementssays that it uses the quotation mark (') as a prefix.openapi-generator/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java
Lines 242 to 245 in fb2c866
But, neither the initializer of the table
openapi-generator/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java
Lines 1836 to 1839 in fb2c866
nor its use-site
DefaultCodegen.toVarNameand its subroutineStringUtils.encodeinsert the quotation mark .openapi-generator/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java
Lines 1612 to 1619 in fb2c866
openapi-generator/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/StringUtils.java
Lines 267 to 285 in fb2c866
Background: I noticed the problem when I was debugging
HaskellServantCodegen.HaskellServantCodegengenerates code that assumes the quotation mark prefixes, but the generated field names do not have the quotation mark prefixes.openapi-generator version
OpenAPI declaration file content or url
Generation Details
Steps to reproduce
Related issues/PRs
Suggest a fix
I guess that the comment is outdated and the current behavior (not prefixed with the quotation mark) is intended behavior.
If so, I suggest simply removing
and prefixed with 'from the comment.