forked from loafle/openapi-generator-original
[all] Add leading slash in path if missing (#1034)
* [all] Add leading slash in path if missing * Fix unit Tests
This commit is contained in:
parent
5fc76ba834
commit
27e343ffef
@ -2254,7 +2254,11 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
}
|
||||
operationId = removeNonNameElementToCamelCase(operationId);
|
||||
|
||||
if(path.startsWith("/")) {
|
||||
op.path = path;
|
||||
} else {
|
||||
op.path = "/" + path;
|
||||
}
|
||||
op.operationId = toOperationId(operationId);
|
||||
op.summary = escapeText(operation.getSummary());
|
||||
op.unescapedNotes = operation.getDescription();
|
||||
|
@ -21,6 +21,7 @@ import io.swagger.parser.OpenAPIParser;
|
||||
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;
|
||||
@ -37,8 +38,14 @@ import org.openapitools.codegen.utils.ModelUtils;
|
||||
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;
|
||||
|
||||
|
||||
@ -224,8 +231,8 @@ public class DefaultCodegenTest {
|
||||
.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"));
|
||||
@ -500,6 +507,21 @@ public class DefaultCodegenTest {
|
||||
});
|
||||
}
|
||||
|
||||
@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");
|
||||
|
@ -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);
|
||||
@ -38,13 +38,13 @@ public class DefaultGeneratorTest {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user