Skip to content

Commit 0fb0cff

Browse files
committed
dots as separator for words in paths
- OData services often use dots (.) to separate long names in their path, so let's use them to improve camel-case flattened names.
1 parent 1956db1 commit 0fb0cff

File tree

3 files changed

+75
-30
lines changed

3 files changed

+75
-30
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,15 +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].replace(".", "_")).append('_');
401+
body.append(normalize(parts[parts.length-2])).append('_');
402402
}
403403
if (parts.length > 1)
404404
{
405-
body.append(parts[parts.length-1].replace(".", "_")).append('_');
405+
body.append(normalize(parts[parts.length-1])).append('_');
406406
}
407407
body.append("body");
408408
return body.toString();
409409
}
410+
411+
private static String normalize(String pathPart)
412+
{
413+
return pathPart.replace(".", "_");
414+
}
410415

411416
private String resolveModelName(String title, String key) {
412417
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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,47 @@ public void resolveInlineRequestBody_maxTwoPathParts() throws Exception {
681681

682682
assertNotNull(bodySchema.getProperties().get("address"));
683683
}
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");
721+
assertTrue(bodySchema instanceof Schema);
722+
723+
assertNotNull(bodySchema.getProperties().get("address"));
724+
}
684725

685726
@Test
686727
public void resolveInlineParameter() throws Exception {

0 commit comments

Comments
 (0)