Skip to content

Commit a18fb4e

Browse files
authored
Merge pull request #3236 from cliffano/master
Disable path HTML-escaping in Ruby api template
2 parents 021f554 + 6de6e93 commit a18fb4e

File tree

5 files changed

+86
-9
lines changed

5 files changed

+86
-9
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public List<File> generate() {
176176
config.additionalProperties().put("termsOfService", config.escapeText(info.getTermsOfService()));
177177
}
178178
}
179-
179+
180180
if(swagger.getVendorExtensions() != null) {
181181
config.vendorExtensions().putAll(swagger.getVendorExtensions());
182182
}
@@ -280,21 +280,21 @@ private Model getParent(Model model) {
280280
Map<String, Object> models = processModels(config, modelMap, definitions);
281281
models.put("classname", config.toModelName(name));
282282
models.putAll(config.additionalProperties());
283-
283+
284284
allProcessedModels.put(name, models);
285285

286286
} catch (Exception e) {
287287
throw new RuntimeException("Could not process model '" + name + "'", e);
288288
}
289289
}
290-
290+
291291
// post process all processed models
292292
allProcessedModels = config.postProcessAllModels(allProcessedModels);
293-
293+
294294
// generate files based on processed models
295295
for (String name: allProcessedModels.keySet()) {
296296
Map<String, Object> models = (Map<String, Object>)allProcessedModels.get(name);
297-
297+
298298
try {
299299
//don't generate models that have an import mapping
300300
if(config.importMapping().containsKey(name)) {
@@ -394,7 +394,7 @@ public int compare(CodegenOperation one, CodegenOperation another) {
394394
operation.put("classname", config.toApiName(tag));
395395
operation.put("classVarName", config.toApiVarName(tag));
396396
operation.put("importPath", config.toApiImport(tag));
397-
397+
398398
if(!config.vendorExtensions().isEmpty()) {
399399
operation.put("vendorExtensions", config.vendorExtensions());
400400
}
@@ -705,7 +705,7 @@ public void processOperation(String resourcePath, String httpMethod, Operation o
705705
tags = new ArrayList<String>();
706706
tags.add("default");
707707
}
708-
708+
709709
/*
710710
build up a set of parameter "ids" defined at the operation level
711711
per the swagger 2.0 spec "A unique parameter is defined by a combination of a name and location"

modules/swagger-codegen/src/main/resources/ruby/api.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ module {{moduleName}}
8989
{{/hasValidation}}
9090
{{/allParams}}
9191
# resource path
92-
local_var_path = "{{path}}".sub('{format}','json'){{#pathParams}}.sub('{' + '{{baseName}}' + '}', {{paramName}}.to_s){{/pathParams}}
92+
local_var_path = "{{{path}}}".sub('{format}','json'){{#pathParams}}.sub('{' + '{{baseName}}' + '}', {{paramName}}.to_s){{/pathParams}}
9393

9494
# query parameters
9595
query_params = {}

modules/swagger-codegen/src/main/resources/ruby/api_client.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ module {{moduleName}}
199199
# @return [Tempfile] the file downloaded
200200
def download_file(response)
201201
content_disposition = response.headers['Content-Disposition']
202-
if content_disposition
202+
if content_disposition and content_disposition =~ /filename=/i
203203
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
204204
prefix = sanitize_filename(filename)
205205
else
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package io.swagger.codegen.ruby;
2+
3+
import io.swagger.codegen.ClientOpts;
4+
import io.swagger.codegen.ClientOptInput;
5+
import io.swagger.codegen.CodegenConfig;
6+
import io.swagger.codegen.DefaultGenerator;
7+
import io.swagger.codegen.languages.RubyClientCodegen;
8+
import io.swagger.models.Swagger;
9+
import io.swagger.parser.SwaggerParser;
10+
11+
import org.apache.commons.io.FileUtils;
12+
import org.junit.rules.TemporaryFolder;
13+
import org.testng.annotations.AfterMethod;
14+
import org.testng.annotations.BeforeMethod;
15+
import org.testng.annotations.Test;
16+
17+
import java.io.File;
18+
import java.nio.charset.StandardCharsets;
19+
import java.util.List;
20+
21+
import static org.junit.Assert.fail;
22+
import static org.testng.Assert.*;
23+
24+
/**
25+
* Tests for RubyClientCodegen-generated templates
26+
*/
27+
public class RubyClientCodegenTest {
28+
29+
public TemporaryFolder folder = new TemporaryFolder();
30+
31+
@BeforeMethod
32+
public void setUp() throws Exception {
33+
folder.create();
34+
}
35+
36+
@AfterMethod
37+
public void tearDown() throws Exception {
38+
folder.delete();
39+
}
40+
41+
@Test
42+
public void testGenerateRubyClientWithHtmlEntity() throws Exception {
43+
final File output = folder.getRoot();
44+
45+
final Swagger swagger = new SwaggerParser().read("src/test/resources/2_0/pathWithHtmlEntity.yaml");
46+
CodegenConfig codegenConfig = new RubyClientCodegen();
47+
codegenConfig.setOutputDir(output.getAbsolutePath());
48+
49+
ClientOptInput clientOptInput = new ClientOptInput().opts(new ClientOpts()).swagger(swagger).config(codegenConfig);
50+
51+
DefaultGenerator generator = new DefaultGenerator();
52+
generator.opts(clientOptInput);
53+
List<File> files = generator.generate();
54+
boolean apiFileGenerated = false;
55+
for (File file : files) {
56+
if (file.getName().equals("default_api.rb")) {
57+
apiFileGenerated = true;
58+
// Ruby client should set the path unescaped in the api file
59+
assertTrue(FileUtils.readFileToString(file, StandardCharsets.UTF_8).contains("local_var_path = \"/foo=bar\""));
60+
}
61+
}
62+
if (!apiFileGenerated) {
63+
fail("Default api file is not generated!");
64+
}
65+
}
66+
67+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
swagger: "2.0"
3+
basePath: "/"
4+
paths:
5+
/foo=bar:
6+
get:
7+
parameters: []
8+
responses:
9+
200:
10+
description: "success"

0 commit comments

Comments
 (0)