Skip to content

Commit 512d574

Browse files
committed
fix for issue #1309
1 parent baf2fe9 commit 512d574

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public Components getComponents(ObjectNode obj, String location, ParseResult res
260260
}
261261
node = getObject("examples",obj,false,location,result);
262262
if(node != null) {
263-
components.setExamples(getExamples(node, String.format("%s.%s", location, "examples"),result));
263+
components.setExamples(getExamples(node, String.format("%s.%s", location, "examples"),result,true));
264264
}
265265

266266
node = getObject("requestBodies",obj,false,location,result);
@@ -992,7 +992,7 @@ public MediaType getMediaType(ObjectNode contentNode, String location, ParseResu
992992

993993
ObjectNode examplesObject = getObject("examples",contentNode,false,location,result);
994994
if(examplesObject!=null) {
995-
mediaType.setExamples(getExamples(examplesObject, String.format("%s.%s", location, "examples"), result));
995+
mediaType.setExamples(getExamples(examplesObject, String.format("%s.%s", location, "examples"), result, false));
996996
}
997997

998998
Object example = getAnyExample("example",contentNode, location,result);
@@ -1565,7 +1565,7 @@ public Parameter getParameter(ObjectNode obj, String location, ParseResult resul
15651565

15661566
ObjectNode examplesObject = getObject("examples",obj,false,location,result);
15671567
if(examplesObject!=null) {
1568-
parameter.setExamples(getExamples(examplesObject, String.format("%s.%s", location, "examples"), result));
1568+
parameter.setExamples(getExamples(examplesObject, String.format("%s.%s", location, "examples"), result,false));
15691569
}
15701570

15711571
Object example = getAnyExample("example", obj, location,result);
@@ -1683,7 +1683,7 @@ public Header getHeader(ObjectNode headerNode, String location, ParseResult resu
16831683

16841684
ObjectNode examplesObject = getObject("examples",headerNode,false,location,result);
16851685
if(examplesObject!=null) {
1686-
header.setExamples(getExamples(examplesObject, location, result));
1686+
header.setExamples(getExamples(examplesObject, location, result, false));
16871687
}
16881688

16891689
Object example = getAnyExample("example", headerNode, location,result);
@@ -2521,17 +2521,19 @@ private byte[] toBytes( String byteString) {
25212521

25222522

25232523

2524-
public Map<String, Example> getExamples(ObjectNode obj, String location, ParseResult result) {
2524+
public Map<String, Example> getExamples(ObjectNode obj, String location, ParseResult result, boolean UnderComponents) {
25252525
if (obj == null) {
25262526
return null;
25272527
}
25282528
Map<String, Example> examples = new LinkedHashMap<>();
25292529

25302530
Set<String> exampleKeys = getKeys(obj);
25312531
for(String exampleName : exampleKeys) {
2532-
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
2533-
exampleName)) {
2534-
result.warning(location, "Example name "+ exampleName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
2532+
if(UnderComponents) {
2533+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
2534+
exampleName)) {
2535+
result.warning(location, "Example name " + exampleName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
2536+
}
25352537
}
25362538

25372539
JsonNode exampleValue = obj.get(exampleName);

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ public class OpenAPIV3ParserTest {
6666
protected int serverPort = getDynamicPort();
6767
protected WireMockServer wireMockServer;
6868

69+
@Test
70+
public void testIssue1309() {
71+
OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
72+
ParseOptions options = new ParseOptions();
73+
options.setResolve(true);
74+
SwaggerParseResult parseResult = openApiParser.readLocation("issue-1309.yaml", null, options);
75+
76+
OpenAPI openAPI = parseResult.getOpenAPI();
77+
assertNotNull(openAPI);
78+
assertEquals(parseResult.getMessages().get(0),"attribute components.schemas.customer-not-found.examples is unexpected");
79+
}
80+
6981
@Test
7082
public void testIssue1316() {
7183
OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
@@ -76,7 +88,6 @@ public void testIssue1316() {
7688
OpenAPI openAPI = parseResult.getOpenAPI();
7789
assertNotNull(openAPI);
7890
assertTrue(parseResult.getMessages().size() == 0);
79-
8091
}
8192

8293

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
openapi: 3.0.1
3+
info:
4+
title: Regular Expression Issue
5+
version: 0.0.2
6+
paths:
7+
"/test":
8+
get:
9+
responses:
10+
'200':
11+
description: It works
12+
'400':
13+
description: Bad Request
14+
content:
15+
application/json:
16+
schema:
17+
type: object
18+
examples:
19+
Entity not found:
20+
summary: Item not existing
21+
components:
22+
schemas:
23+
customer-not-found:
24+
type: object
25+
examples:
26+
Customer not found:
27+
summary: Customer not existing

0 commit comments

Comments
 (0)