Skip to content

Commit 895c99f

Browse files
authored
Merge pull request #1145 from swagger-api/encoding-issue
Fix for encoding issue #1119
2 parents 6157885 + 5de33de commit 895c99f

File tree

3 files changed

+146
-14
lines changed

3 files changed

+146
-14
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/OpenAPIV3Parser.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.slf4j.LoggerFactory;
1919

2020
import java.net.URI;
21+
import java.nio.charset.Charset;
2122
import java.nio.file.Files;
2223
import java.nio.file.Path;
2324
import java.nio.file.Paths;
@@ -30,11 +31,23 @@
3031
public class OpenAPIV3Parser implements SwaggerParserExtension {
3132
private static ObjectMapper JSON_MAPPER, YAML_MAPPER;
3233
private static final Logger LOGGER = LoggerFactory.getLogger(OpenAPIV3Parser.class);
34+
private static String encoding = "UTF-8";
3335

3436
static {
3537
JSON_MAPPER = ObjectMapperFactory.createJson();
3638
YAML_MAPPER = ObjectMapperFactory.createYaml();
3739
}
40+
41+
public static String getEncoding() {
42+
return encoding;
43+
}
44+
45+
public static void setEncoding(String encoding) {
46+
if (Charset.isSupported(encoding)) {
47+
OpenAPIV3Parser.encoding = encoding;
48+
}
49+
}
50+
3851
@Override
3952
public SwaggerParseResult readLocation(String url, List<AuthorizationValue> auth, ParseOptions options) {
4053
SwaggerParseResult result = new SwaggerParseResult();
@@ -130,7 +143,7 @@ public SwaggerParseResult readWithInfo(String location, List<AuthorizationValue>
130143
path = Paths.get(location);
131144
}
132145
if (Files.exists(path)) {
133-
data = FileUtils.readFileToString(path.toFile(), "UTF-8");
146+
data = FileUtils.readFileToString(path.toFile(), encoding);
134147
} else {
135148
data = ClasspathHelper.loadFileFromClasspath(location);
136149
}

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,29 @@ public class OpenAPIV3ParserTest {
6767
protected WireMockServer wireMockServer;
6868

6969

70+
71+
@Test
72+
public void testIssue339() throws Exception {
73+
OpenAPIV3Parser openAPIV3Parser = new OpenAPIV3Parser();
74+
OpenAPI api = openAPIV3Parser.read("issue-339.yaml");
75+
assertNotNull(api);
76+
Parameter param = api.getPaths().get("/store/order/{orderId}").getGet().getParameters().get(0);
77+
assertTrue(param instanceof PathParameter);
78+
PathParameter pp = (PathParameter) param;
79+
assertTrue(pp.getSchema().getMinimum().toString().equals("1"));
80+
assertTrue(pp.getSchema().getMaximum().toString().equals("5"));
81+
}
82+
83+
@Test
84+
public void testIssue1119() {
85+
OpenAPIV3Parser parser = new OpenAPIV3Parser();
86+
parser.setEncoding("ISO-8859-1");
87+
OpenAPI openAPI = parser.read("src/test/resources/issue-1119.yaml");
88+
assertNotNull(openAPI);
89+
assertEquals(openAPI.getPaths().get("/pets").getGet().getParameters().get(0).getDescription(),"Cuántos artículos devolver al mismo tiempo (máximo 100)");
90+
}
91+
92+
7093
@Test
7194
public void testIssue1108() {
7295
OpenAPIV3Parser parser = new OpenAPIV3Parser();
@@ -1337,19 +1360,6 @@ public void testCodegenPetstore() {
13371360
assertEquals(minimum.toString(), "1");
13381361
}
13391362

1340-
@Test
1341-
public void testIssue339() throws Exception {
1342-
OpenAPIV3Parser parser = new OpenAPIV3Parser();
1343-
final OpenAPI openAPI = parser.read("src/test/resources/issue-339.yaml");
1344-
1345-
Parameter param = openAPI.getPaths().get("/store/order/{orderId}").getGet().getParameters().get(0);
1346-
assertTrue(param instanceof PathParameter);
1347-
PathParameter pp = (PathParameter) param;
1348-
1349-
assertTrue(pp.getSchema().getMinimum().toString().equals("1"));
1350-
assertTrue(pp.getSchema().getMaximum().toString().equals("5"));
1351-
}
1352-
13531363
@Test
13541364
public void testCodegenIssue4555() throws Exception {
13551365
OpenAPIV3Parser parser = new OpenAPIV3Parser();
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
license:
6+
name: MIT
7+
servers:
8+
- url: http://petstore.swagger.io/v1
9+
paths:
10+
/pets:
11+
get:
12+
summary: Listado de todas las
13+
operationId: listPets
14+
tags:
15+
- pets
16+
parameters:
17+
- name: limit
18+
in: query
19+
description: Cuántos artículos devolver al mismo tiempo (máximo 100)
20+
required: false
21+
schema:
22+
type: integer
23+
format: int32
24+
responses:
25+
'200':
26+
description: Una colección paginada de mascotas
27+
headers:
28+
x-next:
29+
description: Un enlace a la siguiente página de respuestas.
30+
schema:
31+
type: string
32+
content:
33+
application/json:
34+
schema:
35+
$ref: "#/components/schemas/Pets"
36+
default:
37+
description: Error inesperado
38+
content:
39+
application/json:
40+
schema:
41+
$ref: "#/components/schemas/Error"
42+
post:
43+
summary: Create a pet
44+
operationId: createPets
45+
tags:
46+
- pets
47+
responses:
48+
'201':
49+
description: respuesta Null
50+
default:
51+
description: Error inesperado
52+
content:
53+
application/json:
54+
schema:
55+
$ref: "#/components/schemas/Error"
56+
/pets/{petId}:
57+
get:
58+
summary: Información para una mascota específica
59+
operationId: showPetById
60+
tags:
61+
- pets
62+
parameters:
63+
- name: petId
64+
in: path
65+
required: true
66+
description: El id de la mascota a recuperar.
67+
schema:
68+
type: string
69+
responses:
70+
'200':
71+
description: Respuesta esperada a una solicitud válida
72+
content:
73+
application/json:
74+
schema:
75+
$ref: "#/components/schemas/Pets"
76+
default:
77+
description: error inesperado
78+
content:
79+
application/json:
80+
schema:
81+
$ref: "#/components/schemas/Error"
82+
components:
83+
schemas:
84+
Pet:
85+
required:
86+
- id
87+
- name
88+
properties:
89+
id:
90+
type: integer
91+
format: int64
92+
name:
93+
type: string
94+
tag:
95+
type: string
96+
Pets:
97+
type: array
98+
items:
99+
$ref: "#/components/schemas/Pet"
100+
Error:
101+
required:
102+
- code
103+
- message
104+
properties:
105+
code:
106+
type: integer
107+
format: int32
108+
message:
109+
type: string

0 commit comments

Comments
 (0)