|
16 | 16 |
|
17 | 17 | package org.openapitools.codegen.languages;
|
18 | 18 |
|
| 19 | +import com.google.common.collect.ImmutableMap; |
| 20 | +import com.samskivert.mustache.Escapers; |
| 21 | +import com.samskivert.mustache.Mustache; |
19 | 22 | import io.swagger.v3.oas.models.media.Schema;
|
20 | 23 | import org.openapitools.codegen.*;
|
21 | 24 | import org.openapitools.codegen.meta.features.*;
|
22 | 25 | import org.openapitools.codegen.model.*;
|
| 26 | +import org.openapitools.codegen.templating.mustache.EscapeKeywordLambda; |
23 | 27 | import org.openapitools.codegen.utils.ModelUtils;
|
24 | 28 | import org.slf4j.Logger;
|
25 | 29 | import org.slf4j.LoggerFactory;
|
@@ -557,7 +561,34 @@ public String getHelp() {
|
557 | 561 |
|
558 | 562 | @Override
|
559 | 563 | public String escapeReservedWord(String name) {
|
560 |
| - return "_" + name; |
| 564 | + if (this.reservedWordsMappings().containsKey(name)) { |
| 565 | + return this.reservedWordsMappings().get(name); |
| 566 | + } |
| 567 | + // Reserved words will be further escaped at the mustache compiler level. |
| 568 | + // Scala escaping done here (via `, without compiler escaping) would otherwise be HTML encoded. |
| 569 | + return "`" + name + "`"; |
| 570 | + } |
| 571 | + |
| 572 | + @Override |
| 573 | + public Mustache.Compiler processCompiler(Mustache.Compiler compiler) { |
| 574 | + Mustache.Escaper SCALA = new Mustache.Escaper() { |
| 575 | + @Override |
| 576 | + public String escape(String text) { |
| 577 | + // Fix included as suggested by akkie in #6393 |
| 578 | + // The given text is a reserved word which is escaped by enclosing it with grave accents. If we would |
| 579 | + // escape that with the default Mustache `HTML` escaper, then the escaper would also escape our grave |
| 580 | + // accents. So we remove the grave accents before the escaping and add it back after the escaping. |
| 581 | + if (text.startsWith("`") && text.endsWith("`")) { |
| 582 | + String unescaped = text.substring(1, text.length() - 1); |
| 583 | + return "`" + Escapers.HTML.escape(unescaped) + "`"; |
| 584 | + } |
| 585 | + |
| 586 | + // All none reserved words will be escaped with the default Mustache `HTML` escaper |
| 587 | + return Escapers.HTML.escape(text); |
| 588 | + } |
| 589 | + }; |
| 590 | + |
| 591 | + return compiler.withEscaper(SCALA); |
561 | 592 | }
|
562 | 593 |
|
563 | 594 | @Override
|
@@ -807,12 +838,12 @@ private String cpToPathParameter(CodegenParameter cp, Set<String> imports, Map<S
|
807 | 838 |
|
808 | 839 | if (_vendorExtensions.size() == 1) { // only `x-type`
|
809 | 840 | if ("String".equals(cp.getDataType())) {
|
810 |
| - return cp.paramName; |
| 841 | + return escapeReservedWordUnapply(cp.baseName); |
811 | 842 | } else {
|
812 |
| - return cp.dataType + "Varr(" + cp.paramName + ")"; |
| 843 | + return cp.dataType + "Varr(" + escapeReservedWordUnapply(cp.baseName) + ")"; |
813 | 844 | }
|
814 | 845 | } else {
|
815 |
| - return cp.baseName + "Varr(" + cp.paramName + ")"; |
| 846 | + return cp.baseName + "Varr(" + escapeReservedWordUnapply(cp.baseName) + ")"; |
816 | 847 | }
|
817 | 848 | }
|
818 | 849 |
|
@@ -844,11 +875,23 @@ private String cpToQueryParameter(CodegenParameter cp, Set<String> imports, Map<
|
844 | 875 | }
|
845 | 876 |
|
846 | 877 | vendorExtensions.putAll(refineProp(cp, imports));
|
847 |
| - return cp.baseName + "QueryParam(" + cp.paramName + ")"; |
| 878 | + return cp.baseName + "QueryParam(" + escapeReservedWordUnapply(cp.baseName) + ")"; |
848 | 879 | }
|
849 | 880 |
|
850 | 881 | @Override
|
851 | 882 | public GeneratorLanguage generatorLanguage() {
|
852 | 883 | return GeneratorLanguage.SCALA;
|
853 | 884 | }
|
| 885 | + |
| 886 | + @Override |
| 887 | + protected ImmutableMap.Builder<String, Mustache.Lambda> addMustacheLambdas() { |
| 888 | + return super.addMustacheLambdas() |
| 889 | + .put("escapeReservedWordUnapply", new EscapeKeywordLambda(this::escapeReservedWordUnapply)); |
| 890 | + } |
| 891 | + |
| 892 | + private String escapeReservedWordUnapply(String value) { |
| 893 | + // The unapply method doesn’t allow you to work with reserved variables via backticks; |
| 894 | + // in such cases you should use the variable via a placeholder instead. |
| 895 | + return isReservedWord(value) ? "_" + value : value; |
| 896 | + } |
854 | 897 | }
|
0 commit comments