Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[all] Add leading slash in path if missing #1034

Merged
merged 2 commits into from
Sep 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2254,7 +2254,11 @@ public CodegenOperation fromOperation(String path,
}
operationId = removeNonNameElementToCamelCase(operationId);

op.path = path;
if(path.startsWith("/")) {
op.path = path;
} else {
op.path = "/" + path;
}
op.operationId = toOperationId(operationId);
op.summary = escapeText(operation.getSummary());
op.unescapedNotes = operation.getDescription();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.MediaType;
Expand All @@ -37,8 +38,14 @@
import org.testng.Assert;
import org.testng.annotations.Test;

import java.lang.reflect.Method;
import java.util.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;


Expand Down Expand Up @@ -224,8 +231,8 @@ public void testConsistentParameterNameAfterUniquenessRename() throws Exception
.responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("OK")));

DefaultCodegen codegen = new DefaultCodegen();
CodegenOperation co = codegen.fromOperation("p/", "get", operation, Collections.emptyMap());
Assert.assertEquals(co.path, "p/");
CodegenOperation co = codegen.fromOperation("/some/path", "get", operation, Collections.emptyMap());
Assert.assertEquals(co.path, "/some/path");
Assert.assertEquals(co.allParams.size(), 2);
List<String> allParamsNames = co.allParams.stream().map(p -> p.paramName).collect(Collectors.toList());
Assert.assertTrue(allParamsNames.contains("myparam"));
Expand Down Expand Up @@ -500,6 +507,21 @@ public void testCallbacks() {
});
}

@Test
public void testLeadingSlashIsAddedIfMissing() {
OpenAPI openAPI = TestUtils.createOpenAPI();
Operation operation1 = new Operation().operationId("op1").responses(new ApiResponses().addApiResponse("201", new ApiResponse().description("OK")));
openAPI.path("/here", new PathItem().get(operation1));
Operation operation2 = new Operation().operationId("op2").responses(new ApiResponses().addApiResponse("201", new ApiResponse().description("OK")));
openAPI.path("some/path", new PathItem().get(operation2));
final DefaultCodegen codegen = new DefaultCodegen();

CodegenOperation co1 = codegen.fromOperation("/here", "get", operation2, ModelUtils.getSchemas(openAPI), openAPI);
Assert.assertEquals(co1.path, "/here");
CodegenOperation co2 = codegen.fromOperation("some/path", "get", operation2, ModelUtils.getSchemas(openAPI), openAPI);
Assert.assertEquals(co2.path, "/some/path");
}

private void verifyPersonDiscriminator(CodegenDiscriminator discriminator) {
CodegenDiscriminator test = new CodegenDiscriminator();
test.setPropertyName("$_type");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public class DefaultGeneratorTest {
public void testProcessPaths() throws Exception {
OpenAPI openAPI = TestUtils.createOpenAPI();
openAPI.setPaths(new Paths());
openAPI.getPaths().addPathItem("path1/", new PathItem().get(new Operation().operationId("op1").responses(new ApiResponses().addApiResponse("201", new ApiResponse().description("OK")))));
openAPI.getPaths().addPathItem("path2/", new PathItem().get(new Operation().operationId("op2").addParametersItem(new QueryParameter().name("p1").schema(new StringSchema())).responses(new ApiResponses().addApiResponse("201", new ApiResponse().description("OK")))));
openAPI.getPaths().addPathItem("path3/", new PathItem().addParametersItem(new QueryParameter().name("p1").schema(new StringSchema())).get(new Operation().operationId("op3").addParametersItem(new QueryParameter().name("p2").schema(new IntegerSchema())).responses(new ApiResponses().addApiResponse("201", new ApiResponse().description("OK")))));
openAPI.getPaths().addPathItem("path4/", new PathItem().addParametersItem(new QueryParameter().name("p1").schema(new StringSchema())).get(new Operation().operationId("op4").responses(new ApiResponses().addApiResponse("201", new ApiResponse().description("OK")))));
openAPI.getPaths().addPathItem("/path1", new PathItem().get(new Operation().operationId("op1").responses(new ApiResponses().addApiResponse("201", new ApiResponse().description("OK")))));
openAPI.getPaths().addPathItem("/path2", new PathItem().get(new Operation().operationId("op2").addParametersItem(new QueryParameter().name("p1").schema(new StringSchema())).responses(new ApiResponses().addApiResponse("201", new ApiResponse().description("OK")))));
openAPI.getPaths().addPathItem("/path3", new PathItem().addParametersItem(new QueryParameter().name("p1").schema(new StringSchema())).get(new Operation().operationId("op3").addParametersItem(new QueryParameter().name("p2").schema(new IntegerSchema())).responses(new ApiResponses().addApiResponse("201", new ApiResponse().description("OK")))));
openAPI.getPaths().addPathItem("/path4", new PathItem().addParametersItem(new QueryParameter().name("p1").schema(new StringSchema())).get(new Operation().operationId("op4").responses(new ApiResponses().addApiResponse("201", new ApiResponse().description("OK")))));

ClientOptInput opts = new ClientOptInput();
opts.setOpenAPI(openAPI);
Expand All @@ -38,13 +38,13 @@ public void testProcessPaths() throws Exception {
Assert.assertEquals(result.size(), 1);
List<CodegenOperation> defaultList = result.get("Default");
Assert.assertEquals(defaultList.size(), 4);
Assert.assertEquals(defaultList.get(0).path, "path1/");
Assert.assertEquals(defaultList.get(0).path, "/path1");
Assert.assertEquals(defaultList.get(0).allParams.size(), 0);
Assert.assertEquals(defaultList.get(1).path, "path2/");
Assert.assertEquals(defaultList.get(1).path, "/path2");
Assert.assertEquals(defaultList.get(1).allParams.size(), 1);
Assert.assertEquals(defaultList.get(2).path, "path3/");
Assert.assertEquals(defaultList.get(2).path, "/path3");
Assert.assertEquals(defaultList.get(2).allParams.size(), 2);
Assert.assertEquals(defaultList.get(3).path, "path4/");
Assert.assertEquals(defaultList.get(3).path, "/path4");
Assert.assertEquals(defaultList.get(3).allParams.size(), 1);
}
}