Skip to content

Commit 742ff48

Browse files
committed
[all] Add leading slash in path if missing
1 parent 48e66ed commit 742ff48

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

Diff for: modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -2254,7 +2254,11 @@ public CodegenOperation fromOperation(String path,
22542254
}
22552255
operationId = removeNonNameElementToCamelCase(operationId);
22562256

2257-
op.path = path;
2257+
if(path.startsWith("/")) {
2258+
op.path = path;
2259+
} else {
2260+
op.path = "/" + path;
2261+
}
22582262
op.operationId = toOperationId(operationId);
22592263
op.summary = escapeText(operation.getSummary());
22602264
op.unescapedNotes = operation.getDescription();

Diff for: modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.swagger.v3.oas.models.Components;
2222
import io.swagger.v3.oas.models.OpenAPI;
2323
import io.swagger.v3.oas.models.Operation;
24+
import io.swagger.v3.oas.models.PathItem;
2425
import io.swagger.v3.oas.models.media.ArraySchema;
2526
import io.swagger.v3.oas.models.media.Content;
2627
import io.swagger.v3.oas.models.media.MediaType;
@@ -37,7 +38,6 @@
3738
import org.testng.Assert;
3839
import org.testng.annotations.Test;
3940

40-
import java.lang.reflect.Method;
4141
import java.util.*;
4242
import java.util.stream.Collectors;
4343

@@ -500,6 +500,21 @@ public void testCallbacks() {
500500
});
501501
}
502502

503+
@Test
504+
public void testLeadingSlashIsAddedIfMissing() {
505+
OpenAPI openAPI = TestUtils.createOpenAPI();
506+
Operation operation1 = new Operation().operationId("op1").responses(new ApiResponses().addApiResponse("201", new ApiResponse().description("OK")));
507+
openAPI.path("/here", new PathItem().get(operation1));
508+
Operation operation2 = new Operation().operationId("op2").responses(new ApiResponses().addApiResponse("201", new ApiResponse().description("OK")));
509+
openAPI.path("some/path", new PathItem().get(operation2));
510+
final DefaultCodegen codegen = new DefaultCodegen();
511+
512+
CodegenOperation co1 = codegen.fromOperation("/here", "get", operation2, ModelUtils.getSchemas(openAPI), openAPI);
513+
Assert.assertEquals(co1.path, "/here");
514+
CodegenOperation co2 = codegen.fromOperation("some/path", "get", operation2, ModelUtils.getSchemas(openAPI), openAPI);
515+
Assert.assertEquals(co2.path, "/some/path");
516+
}
517+
503518
private void verifyPersonDiscriminator(CodegenDiscriminator discriminator) {
504519
CodegenDiscriminator test = new CodegenDiscriminator();
505520
test.setPropertyName("$_type");

0 commit comments

Comments
 (0)