Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit b51eb63

Browse files
committed
Updates java code to return pathPieces from paths and servers
1 parent c551007 commit b51eb63

File tree

2 files changed

+73
-28
lines changed

2 files changed

+73
-28
lines changed

src/main/java/org/openapijsonschematools/codegen/generators/DefaultGenerator.java

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3812,18 +3812,18 @@ protected void updateComponentsFilepath(String[] pathPieces) {
38123812
}
38133813
}
38143814

3815-
private void updatePathsFilepath(String[] pathPieces) {
3815+
private String[] updatePathsFilepath(String[] pathPieces) {
38163816
String[] originalPieces = pathPieces.clone();
38173817
originalPieces[0] = "#";
38183818
String jsonPath = String.join("/", originalPieces);
38193819
if (pathPieces.length < 3) {
3820-
return;
3820+
return pathPieces;
38213821
}
38223822
// #/paths/somePath
38233823
String path = pathPieces[2];
38243824
pathPieces[2] = toPathFilename(ModelUtils.decodeSlashes(pathPieces[2]), null);
38253825
if (pathPieces.length < 4) {
3826-
return;
3826+
return pathPieces;
38273827
}
38283828
Set<String> xParameters = new HashSet<>();
38293829
xParameters.add("PathParameters");
@@ -3842,11 +3842,11 @@ private void updatePathsFilepath(String[] pathPieces) {
38423842
pathPieces[4] = "server" + pathPieces[4];
38433843
pathPieces[5] = "Variables";
38443844
}
3845-
return;
3845+
return pathPieces;
38463846
} else if (pathPieces[3].equals("parameters")) {
38473847
if (pathPieces.length == 4) {
38483848
// #/paths/somePath/parameters
3849-
return;
3849+
return pathPieces;
38503850
}
38513851
// #/paths/somePath/parameters/0
38523852
pathPieces[4] = toParameterFilename(pathPieces[4], null);
@@ -3856,16 +3856,16 @@ private void updatePathsFilepath(String[] pathPieces) {
38563856
pathPieces[6] = toContentTypeFilename(contentType);
38573857
if (pathPieces.length == 8) {
38583858
pathPieces[7] = getSchemaFilename(jsonPath);
3859-
return;
3859+
return pathPieces;
38603860
}
38613861
} else if (pathPieces.length == 6 && pathPieces[5].equals("schema")) {
38623862
// #/paths/somePath/parameters/0/schema -> length 7
38633863
pathPieces[5] = getSchemaFilename(jsonPath);
3864-
return;
3864+
return pathPieces;
38653865
}
38663866
} else if (pathPieces.length == 4) {
38673867
// #/paths/SomePath/get
3868-
return;
3868+
return pathPieces;
38693869
}
38703870
if (pathPieces[4].equals("requestBody")) {
38713871
// #/paths/somePath/get/requestBody
@@ -3895,11 +3895,11 @@ private void updatePathsFilepath(String[] pathPieces) {
38953895
// #/paths/somePath/get/security/0
38963896
pathPieces[5] = toSecurityRequirementObjectFilename(pathPieces[5], jsonPath);
38973897
}
3898-
return;
3898+
return pathPieces;
38993899
} else if (pathPieces[4].equals("responses")) {
39003900
if (pathPieces.length < 6) {
39013901
// #/paths/user_login/get/responses -> length 5
3902-
return;
3902+
return pathPieces;
39033903
}
39043904
// #/paths/user_login/get/responses/200 -> 200 -> response_200 -> length 6
39053905
String responseJsonPath = "#/paths/" + path + "/" + pathPieces[3] + "/responses/" + pathPieces[5];
@@ -3908,11 +3908,11 @@ private void updatePathsFilepath(String[] pathPieces) {
39083908
// synthetic json path
39093909
// #/paths/user_login/get/responses/200/Headers
39103910
pathPieces[6] = getSchemaFilename(jsonPath);
3911-
return;
3911+
return pathPieces;
39123912
}
39133913

39143914
if (pathPieces.length < 8) {
3915-
return;
3915+
return pathPieces;
39163916
}
39173917
if (pathPieces[6].equals("content")) {
39183918
// #/paths/somePath/get/responses/200/content/application-json -> length 8
@@ -3938,7 +3938,7 @@ private void updatePathsFilepath(String[] pathPieces) {
39383938
}
39393939
} else if (pathPieces[4].equals("parameters")) {
39403940
if (pathPieces.length == 5) {
3941-
return;
3941+
return pathPieces;
39423942
}
39433943
// #/paths/somePath/get/parameters/0 -> length 6
39443944
pathPieces[5] = toParameterFilename(pathPieces[5], null);
@@ -3965,6 +3965,7 @@ private void updatePathsFilepath(String[] pathPieces) {
39653965
}
39663966
}
39673967
}
3968+
return pathPieces;
39683969
}
39693970

39703971
protected void updateServersFilepath(String[] pathPieces) {
@@ -3981,15 +3982,17 @@ protected void updateServersFilepath(String[] pathPieces) {
39813982
}
39823983
}
39833984

3984-
private void updateSecurityFilepath(String[] pathPieces) {
3985+
protected String[] updateSecurityFilepath(String[] pathPieces) {
3986+
List<String> finalPathPieces = Arrays.asList(pathPieces);
39853987
String jsonPath = String.join("/", pathPieces);
39863988
if (pathPieces.length < 3) {
39873989
// #/security
3988-
pathPieces[1] = toSecurityFilename("", jsonPath);
3989-
return;
3990+
finalPathPieces.set(1, toSecurityFilename("", jsonPath));
3991+
return finalPathPieces.toArray(String[]::new);
39903992
}
39913993
// #/security/0
3992-
pathPieces[2] = toSecurityRequirementObjectFilename(pathPieces[2], jsonPath);
3994+
finalPathPieces.set(2, toSecurityRequirementObjectFilename(pathPieces[2], jsonPath));
3995+
return finalPathPieces.toArray(String[]::new);
39933996
}
39943997

39953998
private void updateApisFilepath(String[] pathPieces) {
@@ -4013,19 +4016,25 @@ private void updateApisFilepath(String[] pathPieces) {
40134016
public String getFilepath(String jsonPath) {
40144017
String[] pathPieces = jsonPath.split("/");
40154018
pathPieces[0] = outputFolder + File.separatorChar + packagePath();
4019+
String[] usedPathPieces;
40164020
if (jsonPath.startsWith("#/components")) {
40174021
updateComponentsFilepath(pathPieces);
4022+
usedPathPieces = pathPieces;
40184023
} else if (jsonPath.startsWith("#/paths")) {
4019-
updatePathsFilepath(pathPieces);
4024+
usedPathPieces = updatePathsFilepath(pathPieces);
40204025
} else if (jsonPath.startsWith("#/servers")) {
40214026
updateServersFilepath(pathPieces);
4027+
usedPathPieces = pathPieces;
40224028
} else if (jsonPath.startsWith("#/security")) {
4023-
updateSecurityFilepath(pathPieces);
4029+
usedPathPieces = updateSecurityFilepath(pathPieces);
40244030
} else if (jsonPath.startsWith("#/apis")) {
40254031
// this is a fake json path that the code generates and uses to generate apis
40264032
updateApisFilepath(pathPieces);
4033+
usedPathPieces = pathPieces;
4034+
} else {
4035+
throw new RuntimeException("invalid jsonpath value");
40274036
}
4028-
List<String> finalPathPieces = Arrays.stream(pathPieces)
4037+
List<String> finalPathPieces = Arrays.stream(usedPathPieces)
40294038
.filter(Objects::nonNull)
40304039
.collect(Collectors.toList());
40314040
return String.join(File.separator, finalPathPieces);
@@ -4035,19 +4044,25 @@ public String getFilepath(String jsonPath) {
40354044
public String getSubpackage(String jsonPath) {
40364045
String[] pathPieces = jsonPath.split("/");
40374046
pathPieces[0] = "";
4047+
String[] usedPathPieces;
40384048
if (jsonPath.startsWith("#/components")) {
40394049
updateComponentsFilepath(pathPieces);
4050+
usedPathPieces = pathPieces;
40404051
} else if (jsonPath.startsWith("#/paths")) {
4041-
updatePathsFilepath(pathPieces);
4052+
usedPathPieces = updatePathsFilepath(pathPieces);
40424053
} else if (jsonPath.startsWith("#/servers")) {
40434054
updateServersFilepath(pathPieces);
4055+
usedPathPieces = pathPieces;
40444056
} else if (jsonPath.startsWith("#/security")) {
4045-
updateSecurityFilepath(pathPieces);
4057+
usedPathPieces = updateSecurityFilepath(pathPieces);
40464058
} else if (jsonPath.startsWith("#/apis")) {
40474059
// this is a fake json path that the code generates and uses to generate apis
40484060
updateApisFilepath(pathPieces);
4061+
usedPathPieces = pathPieces;
4062+
} else {
4063+
throw new RuntimeException("invalid value for jsonPath");
40494064
}
4050-
List<String> finalPathPieces = Arrays.stream(pathPieces)
4065+
List<String> finalPathPieces = Arrays.stream(usedPathPieces)
40514066
.filter(Objects::nonNull)
40524067
.collect(Collectors.toList());
40534068
String subpackage = String.join(".", finalPathPieces);
@@ -4061,22 +4076,26 @@ public String getSubpackage(String jsonPath) {
40614076
public String getTestFilepath(String jsonPath) {
40624077
String[] pathPieces = jsonPath.split("/");
40634078
pathPieces[0] = outputFolder + File.separatorChar + "test";
4079+
String[] usedPathPieces;
40644080
if (jsonPath.startsWith("#/components")) {
40654081
// #/components/schemas/someSchema
40664082
updateComponentsFilepath(pathPieces);
40674083
if (pathPieces.length == 4) {
40684084
int lastIndex = pathPieces.length - 1;
40694085
pathPieces[lastIndex] = "test_" + pathPieces[lastIndex];
40704086
}
4087+
usedPathPieces = pathPieces;
40714088
} else if (jsonPath.startsWith("#/paths")) {
4072-
updatePathsFilepath(pathPieces);
4089+
usedPathPieces = updatePathsFilepath(pathPieces);
40734090
// #/paths/somePath/get
40744091
if (pathPieces.length == 4) {
40754092
int lastIndex = pathPieces.length - 1;
4076-
pathPieces[lastIndex] = "test_" + pathPieces[lastIndex];
4093+
usedPathPieces[lastIndex] = "test_" + pathPieces[lastIndex];
40774094
}
4095+
} else {
4096+
throw new RuntimeException("Invalid value for jsonPath");
40784097
}
4079-
List<String> finalPathPieces = Arrays.stream(pathPieces)
4098+
List<String> finalPathPieces = Arrays.stream(usedPathPieces)
40804099
.filter(Objects::nonNull)
40814100
.collect(Collectors.toList());
40824101
return String.join(File.separator, finalPathPieces);
@@ -4087,17 +4106,23 @@ public String getTestFilepath(String jsonPath) {
40874106
public String getDocsFilepath(String jsonPath) {
40884107
String[] pathPieces = jsonPath.split("/");
40894108
pathPieces[0] = outputFolder + File.separatorChar + docsFolder;
4109+
String[] usedPathPieces;
40904110
if (jsonPath.startsWith("#/components")) {
40914111
updateComponentsFilepath(pathPieces);
4112+
usedPathPieces = pathPieces;
40924113
} else if (jsonPath.startsWith("#/paths")) {
4093-
updatePathsFilepath(pathPieces);
4114+
usedPathPieces = updatePathsFilepath(pathPieces);
40944115
} else if (jsonPath.startsWith("#/servers")) {
40954116
updateServersFilepath(pathPieces);
4117+
usedPathPieces = pathPieces;
40964118
} else if (jsonPath.startsWith("#/apis")) {
40974119
// this is a fake json path that the code generates and uses to generate apis
40984120
updateApisFilepath(pathPieces);
4121+
usedPathPieces = pathPieces;
4122+
} else {
4123+
throw new RuntimeException("invalid json path");
40994124
}
4100-
List<String> finalPathPieces = Arrays.stream(pathPieces)
4125+
List<String> finalPathPieces = Arrays.stream(usedPathPieces)
41014126
.filter(Objects::nonNull)
41024127
.collect(Collectors.toList());
41034128
return String.join(File.separator, finalPathPieces);

src/main/java/org/openapijsonschematools/codegen/generators/JavaClientGenerator.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2804,4 +2804,24 @@ public CodegenSchema fromSchema(Schema p, String sourceJsonPath, String currentJ
28042804
}
28052805
return cp;
28062806
}
2807+
2808+
/*
2809+
custom version of this method so the security file can be moved into the
2810+
package containing the security requirements objects
2811+
This is needed to allow sealed classes to be generated for those SecurityRequirementObjects
2812+
And those sealed classes are needed to build a constructor that requires one
2813+
SecurityRequirementObject and allows additional SecurityRequirementObjects
2814+
*/
2815+
protected String[] updateSecurityFilepath(String[] pathPieces) {
2816+
List<String> finalPathPieces = Arrays.asList(pathPieces);
2817+
String jsonPath = String.join("/", pathPieces);
2818+
if (pathPieces.length < 3) {
2819+
// #/security
2820+
finalPathPieces.add(toSecurityFilename("", jsonPath));
2821+
return finalPathPieces.toArray(String[]::new);
2822+
}
2823+
// #/security/0
2824+
finalPathPieces.set(2, toSecurityRequirementObjectFilename(pathPieces[2], jsonPath));
2825+
return finalPathPieces.toArray(String[]::new);
2826+
}
28072827
}

0 commit comments

Comments
 (0)