Skip to content

Commit bb28d1d

Browse files
authored
Merge pull request #1564 from ivy-rew/bug/1562-pathBody
Bug/1562 path body
2 parents 3ef6f78 + 0fb0cff commit bb28d1d

File tree

3 files changed

+86
-38
lines changed

3 files changed

+86
-38
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,12 +398,20 @@ private static String pathBody(String pathname)
398398
StringBuilder body = new StringBuilder();
399399
if (parts.length > 2)
400400
{
401-
body.append(parts[parts.length-2]);
401+
body.append(normalize(parts[parts.length-2])).append('_');
402402
}
403-
body.append(parts[parts.length-1]);
404-
body.append("Body");
403+
if (parts.length > 1)
404+
{
405+
body.append(normalize(parts[parts.length-1])).append('_');
406+
}
407+
body.append("body");
405408
return body.toString();
406409
}
410+
411+
private static String normalize(String pathPart)
412+
{
413+
return pathPart.replace(".", "_");
414+
}
407415

408416
private String resolveModelName(String title, String key) {
409417
if (title == null) {

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

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
package io.swagger.v3.parser.test;
22

33

4+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
5+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
6+
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
7+
import static java.util.Arrays.asList;
8+
import static java.util.Collections.emptyList;
9+
import static org.hamcrest.CoreMatchers.equalTo;
10+
import static org.hamcrest.CoreMatchers.instanceOf;
11+
import static org.hamcrest.CoreMatchers.notNullValue;
12+
import static org.junit.Assert.assertThat;
13+
import static org.testng.Assert.assertEquals;
14+
import static org.testng.Assert.assertFalse;
15+
import static org.testng.Assert.assertNotNull;
16+
import static org.testng.Assert.assertNull;
17+
import static org.testng.Assert.assertTrue;
18+
import static org.testng.Assert.fail;
19+
420
import java.io.File;
521
import java.io.IOException;
622
import java.math.BigDecimal;
@@ -14,11 +30,20 @@
1430
import java.util.Random;
1531
import java.util.Set;
1632

33+
import org.apache.commons.io.FileUtils;
34+
import org.hamcrest.CoreMatchers;
35+
import org.testng.Assert;
36+
import org.testng.annotations.AfterClass;
37+
import org.testng.annotations.BeforeClass;
38+
import org.testng.annotations.Test;
39+
import org.testng.reporters.Files;
40+
1741
import com.fasterxml.jackson.databind.JsonNode;
1842
import com.fasterxml.jackson.databind.node.ObjectNode;
1943
import com.github.tomakehurst.wiremock.WireMockServer;
2044
import com.github.tomakehurst.wiremock.client.WireMock;
2145
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
46+
2247
import io.swagger.v3.core.util.Json;
2348
import io.swagger.v3.core.util.Yaml;
2449
import io.swagger.v3.oas.models.Components;
@@ -52,32 +77,6 @@
5277
import io.swagger.v3.parser.core.models.ParseOptions;
5378
import io.swagger.v3.parser.core.models.SwaggerParseResult;
5479
import mockit.Injectable;
55-
import org.apache.commons.io.FileUtils;
56-
import org.apache.commons.lang3.StringUtils;
57-
import org.hamcrest.CoreMatchers;
58-
import org.testng.Assert;
59-
import org.testng.annotations.AfterClass;
60-
import org.testng.annotations.BeforeClass;
61-
import org.testng.annotations.Test;
62-
import org.testng.reporters.Files;
63-
64-
import static java.util.Arrays.asList;
65-
import static java.util.Collections.emptyList;
66-
67-
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
68-
import static com.github.tomakehurst.wiremock.client.WireMock.get;
69-
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
70-
import static org.hamcrest.CoreMatchers.equalTo;
71-
import static org.hamcrest.CoreMatchers.instanceOf;
72-
import static org.hamcrest.CoreMatchers.notNullValue;
73-
import static org.junit.Assert.assertNotEquals;
74-
import static org.junit.Assert.assertThat;
75-
import static org.testng.Assert.assertEquals;
76-
import static org.testng.Assert.assertFalse;
77-
import static org.testng.Assert.assertNotNull;
78-
import static org.testng.Assert.assertNull;
79-
import static org.testng.Assert.assertTrue;
80-
import static org.testng.Assert.fail;
8180

8281

8382
public class OpenAPIV3ParserTest {
@@ -416,9 +415,9 @@ public void testCodegenIssue8601() {
416415

417416
OpenAPI openAPI = parseResult.getOpenAPI();
418417
assertNotNull(openAPI.getComponents().getSchemas().get("status"));
419-
assertNotNull(openAPI.getComponents().getSchemas().get("testBody"));
418+
assertNotNull(openAPI.getComponents().getSchemas().get("test_body"));
420419
assertNotNull(openAPI.getComponents().getSchemas().get("inline_response_200"));
421-
assertNotNull(openAPI.getComponents().getSchemas().get("testBody_1"));
420+
assertNotNull(openAPI.getComponents().getSchemas().get("test_body_1"));
422421
assertNotNull(openAPI.getComponents().getSchemas().get("Test1"));
423422
assertNotNull(openAPI.getComponents().getSchemas().get("Test2"));
424423
}

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/InlineModelResolverTest.java

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ public void resolveInlineRequestBody() throws Exception {
637637
RequestBody body = getOperation.getRequestBody();
638638
assertTrue(body.getContent().get("*/*").getSchema().get$ref() != null);
639639

640-
Schema bodySchema = openAPI.getComponents().getSchemas().get("helloBody");
640+
Schema bodySchema = openAPI.getComponents().getSchemas().get("hello_body");
641641
assertTrue(bodySchema instanceof Schema);
642642

643643
assertNotNull(bodySchema.getProperties().get("address"));
@@ -676,7 +676,48 @@ public void resolveInlineRequestBody_maxTwoPathParts() throws Exception {
676676
RequestBody body = getOperation.getRequestBody();
677677
assertTrue(body.getContent().get("*/*").getSchema().get$ref() != null);
678678

679-
Schema bodySchema = openAPI.getComponents().getSchemas().get("greethelloBody");
679+
Schema bodySchema = openAPI.getComponents().getSchemas().get("greet_hello_body");
680+
assertTrue(bodySchema instanceof Schema);
681+
682+
assertNotNull(bodySchema.getProperties().get("address"));
683+
}
684+
685+
@Test
686+
public void resolveInlineRequestBody_stripsDotsFromPath() throws Exception {
687+
OpenAPI openAPI = new OpenAPI();
688+
689+
ObjectSchema objectSchema = new ObjectSchema();
690+
objectSchema.addProperties("street", new StringSchema());
691+
692+
Schema schema = new Schema();
693+
schema.addProperties("address", objectSchema);
694+
schema.addProperties("name", new StringSchema());
695+
696+
MediaType mediaType = new MediaType();
697+
mediaType.setSchema(schema);
698+
699+
Content content = new Content();
700+
content.addMediaType("*/*", mediaType );
701+
702+
RequestBody requestBody = new RequestBody();
703+
requestBody.setContent(content);
704+
705+
Operation operation = new Operation();
706+
operation.setRequestBody(requestBody);
707+
708+
PathItem pathItem = new PathItem();
709+
pathItem.setGet(operation);
710+
openAPI.path("/api/Cloud.Greet.Hello",pathItem);
711+
712+
new InlineModelResolver(true, true).flatten(openAPI);
713+
714+
Operation getOperation = openAPI.getPaths().get("/api/Cloud.Greet.Hello").getGet();
715+
RequestBody body = getOperation.getRequestBody();
716+
assertEquals("use dot as common word separator: as it occurs frequently on OData services",
717+
"#/components/schemas/ApiCloudGreetHelloBody",
718+
body.getContent().get("*/*").getSchema().get$ref());
719+
720+
Schema bodySchema = openAPI.getComponents().getSchemas().get("ApiCloudGreetHelloBody");
680721
assertTrue(bodySchema instanceof Schema);
681722

682723
assertNotNull(bodySchema.getProperties().get("address"));
@@ -805,9 +846,9 @@ public void resolveInlineArrayRequestBody() throws Exception {
805846
Schema inner = am.getItems();
806847
assertTrue(inner.get$ref() != null);
807848

808-
assertEquals( "#/components/schemas/helloBody",inner.get$ref());
849+
assertEquals( "#/components/schemas/hello_body",inner.get$ref());
809850

810-
Schema inline = openAPI.getComponents().getSchemas().get("helloBody");
851+
Schema inline = openAPI.getComponents().getSchemas().get("hello_body");
811852
assertNotNull(inline);
812853
assertTrue(inline instanceof Schema);
813854

@@ -1091,7 +1132,7 @@ public void testArbitraryObjectRequestBodyInline() {
10911132
RequestBody requestBody = operation.getRequestBody();
10921133
assertTrue(requestBody.getContent().get("*/*").getSchema().get$ref() != null);
10931134

1094-
Schema body = swagger.getComponents().getSchemas().get("helloBody");
1135+
Schema body = swagger.getComponents().getSchemas().get("hello_body");
10951136
assertTrue(body instanceof Schema);
10961137

10971138

@@ -1153,9 +1194,9 @@ public void testArbitraryObjectBodyParamArrayInline() {
11531194
assertTrue(inner.get$ref() != null);
11541195

11551196

1156-
assertEquals(inner.get$ref(), "#/components/schemas/helloBody");
1197+
assertEquals(inner.get$ref(), "#/components/schemas/hello_body");
11571198

1158-
Schema inline = openAPI.getComponents().getSchemas().get("helloBody");
1199+
Schema inline = openAPI.getComponents().getSchemas().get("hello_body");
11591200
assertNotNull(inline);
11601201

11611202
Schema p = (Schema)inline.getProperties().get("arbitrary");

0 commit comments

Comments
 (0)