Skip to content

[BUG] DefaultCodegen.specialCharReplacements comment about prefixing with the quotation mark #16161

@msakai

Description

@msakai

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions