|
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;
|
@@ -353,8 +357,7 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
|
353 | 357 |
|
354 | 358 | if (!cModel.oneOf.isEmpty()) {
|
355 | 359 | cModel.getVendorExtensions().put("x-isSealedTrait", true);
|
356 |
| - } |
357 |
| - else if (cModel.isEnum) { |
| 360 | + } else if (cModel.isEnum) { |
358 | 361 | cModel.getVendorExtensions().put("x-isEnum", true);
|
359 | 362 |
|
360 | 363 | } else {
|
@@ -557,7 +560,34 @@ public String getHelp() {
|
557 | 560 |
|
558 | 561 | @Override
|
559 | 562 | public String escapeReservedWord(String name) {
|
560 |
| - return "_" + name; |
| 563 | + if (this.reservedWordsMappings().containsKey(name)) { |
| 564 | + return this.reservedWordsMappings().get(name); |
| 565 | + } |
| 566 | + // Reserved words will be further escaped at the mustache compiler level. |
| 567 | + // Scala escaping done here (via `, without compiler escaping) would otherwise be HTML encoded. |
| 568 | + return "`" + name + "`"; |
| 569 | + } |
| 570 | + |
| 571 | + @Override |
| 572 | + public Mustache.Compiler processCompiler(Mustache.Compiler compiler) { |
| 573 | + Mustache.Escaper SCALA = new Mustache.Escaper() { |
| 574 | + @Override |
| 575 | + public String escape(String text) { |
| 576 | + // Fix included as suggested by akkie in #6393 |
| 577 | + // The given text is a reserved word which is escaped by enclosing it with grave accents. If we would |
| 578 | + // escape that with the default Mustache `HTML` escaper, then the escaper would also escape our grave |
| 579 | + // accents. So we remove the grave accents before the escaping and add it back after the escaping. |
| 580 | + if (text.startsWith("`") && text.endsWith("`")) { |
| 581 | + String unescaped = text.substring(1, text.length() - 1); |
| 582 | + return "`" + Escapers.HTML.escape(unescaped) + "`"; |
| 583 | + } |
| 584 | + |
| 585 | + // All none reserved words will be escaped with the default Mustache `HTML` escaper |
| 586 | + return Escapers.HTML.escape(text); |
| 587 | + } |
| 588 | + }; |
| 589 | + |
| 590 | + return compiler.withEscaper(SCALA); |
561 | 591 | }
|
562 | 592 |
|
563 | 593 | @Override
|
@@ -807,12 +837,12 @@ private String cpToPathParameter(CodegenParameter cp, Set<String> imports, Map<S
|
807 | 837 |
|
808 | 838 | if (_vendorExtensions.size() == 1) { // only `x-type`
|
809 | 839 | if ("String".equals(cp.getDataType())) {
|
810 |
| - return cp.paramName; |
| 840 | + return escapeReservedWordUnapply(cp.baseName); |
811 | 841 | } else {
|
812 |
| - return cp.dataType + "Varr(" + cp.paramName + ")"; |
| 842 | + return cp.dataType + "Varr(" + escapeReservedWordUnapply(cp.baseName) + ")"; |
813 | 843 | }
|
814 | 844 | } else {
|
815 |
| - return cp.baseName + "Varr(" + cp.paramName + ")"; |
| 845 | + return cp.baseName + "Varr(" + escapeReservedWordUnapply(cp.baseName) + ")"; |
816 | 846 | }
|
817 | 847 | }
|
818 | 848 |
|
@@ -844,11 +874,23 @@ private String cpToQueryParameter(CodegenParameter cp, Set<String> imports, Map<
|
844 | 874 | }
|
845 | 875 |
|
846 | 876 | vendorExtensions.putAll(refineProp(cp, imports));
|
847 |
| - return cp.baseName + "QueryParam(" + cp.paramName + ")"; |
| 877 | + return cp.baseName + "QueryParam(" + escapeReservedWordUnapply(cp.baseName) + ")"; |
848 | 878 | }
|
849 | 879 |
|
850 | 880 | @Override
|
851 | 881 | public GeneratorLanguage generatorLanguage() {
|
852 | 882 | return GeneratorLanguage.SCALA;
|
853 | 883 | }
|
| 884 | + |
| 885 | + @Override |
| 886 | + protected ImmutableMap.Builder<String, Mustache.Lambda> addMustacheLambdas() { |
| 887 | + return super.addMustacheLambdas() |
| 888 | + .put("escapeReservedWordUnapply", new EscapeKeywordLambda(this::escapeReservedWordUnapply)); |
| 889 | + } |
| 890 | + |
| 891 | + private String escapeReservedWordUnapply(String value) { |
| 892 | + // The unapply method doesn’t allow you to work with reserved variables via backticks; |
| 893 | + // in such cases you should use the variable via a placeholder instead. |
| 894 | + return isReservedWord(value) ? "_" + value : value; |
| 895 | + } |
854 | 896 | }
|
0 commit comments