Skip to content

Commit 7ba2a39

Browse files
committed
Replace dashes in method name. Support parameters on path level.
1 parent c375c08 commit 7ba2a39

File tree

3 files changed

+58
-31
lines changed

3 files changed

+58
-31
lines changed

operator-gen-maven-plugin/src/main/java/org/acme/client/KiotaMethodCallFactory.java

+16
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ private MethodCallExpr toMethodCallExpression(Expression prev, String pathKey, N
7070
MethodCallExpr methodCallExpr;
7171
if (methodName.startsWith("{")) {
7272
methodName = "by" + Character.toUpperCase(methodName.charAt(1)) + methodName.substring(2, methodName.length() - 1);
73+
methodName = replaceDashes(methodName);
7374
methodCallExpr = new MethodCallExpr(prev, methodName, args.size() > 1 ? new NodeList<>(args.remove(0)) : args);
7475
} else {
7576
methodCallExpr = new MethodCallExpr(prev, methodName);
@@ -80,5 +81,20 @@ private MethodCallExpr toMethodCallExpression(Expression prev, String pathKey, N
8081
return methodCallExpr;
8182
}
8283
}
84+
85+
private static String replaceDashes(String methodName) {
86+
int dashIndex = methodName.indexOf("-");
87+
while(dashIndex > -1) {
88+
methodName = methodName.substring(0, dashIndex)
89+
+ String.valueOf(methodName.charAt(dashIndex + 1)).toUpperCase()
90+
+ methodName.substring(dashIndex + 2, methodName.length());
91+
dashIndex = methodName.indexOf("-");
92+
}
93+
return methodName;
94+
}
95+
96+
public static void main(String[] args) {
97+
System.out.println(replaceDashes("abcd-efg"));
98+
}
8399

84100
}

operator-gen-maven-plugin/src/main/java/org/acme/client/ParameterResolver.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public Optional<Parameter> getParameterType(String path, int paramIndex) {
3434
Optional<Operation> oneOfIdOps = Optional.ofNullable(pathItem.getGET())
3535
.or(() -> Optional.ofNullable(pathItem.getPATCH()))
3636
.or(() -> Optional.ofNullable(pathItem.getDELETE()));
37-
return oneOfIdOps.map(o -> o.getParameters().get(paramIndex));
37+
return oneOfIdOps.map(o -> o.getParameters()).or(() -> Optional.ofNullable(pathItem.getParameters()))
38+
.map(p -> p.get(paramIndex));
3839
}
3940

4041
public NodeList<Expression> resolveArgs(String path, NameExpr primary, NodeList<Expression> defaultArgs) {

operator-gen-maven-plugin/src/main/java/org/acme/gen/DependentGen.java

+40-30
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.acme.client.ApiClientMethodCallFactory;
1111
import org.acme.read.crud.CrudMapper;
1212
import org.eclipse.microprofile.openapi.models.Operation;
13+
import org.eclipse.microprofile.openapi.models.media.Schema;
1314

1415
import com.github.javaparser.ast.CompilationUnit;
1516
import com.github.javaparser.ast.Modifier.Keyword;
@@ -41,7 +42,6 @@
4142
import com.github.javaparser.ast.stmt.ThrowStmt;
4243
import com.github.javaparser.ast.stmt.TryStmt;
4344
import com.github.javaparser.ast.type.ClassOrInterfaceType;
44-
import com.github.javaparser.ast.type.Type;
4545
import com.github.javaparser.ast.type.TypeParameter;
4646
import com.github.javaparser.utils.SourceRoot;
4747
import com.microsoft.kiota.ApiException;
@@ -58,8 +58,6 @@
5858
import io.kiota.http.vertx.VertXRequestAdapter;
5959
import io.kiota.serialization.json.JsonParseNodeFactory;
6060
import io.vertx.core.Vertx;
61-
import io.vertx.ext.web.client.WebClient;
62-
import io.vertx.ext.web.client.WebClientSession;
6361
import jakarta.annotation.PostConstruct;
6462
import jakarta.inject.Inject;
6563

@@ -101,8 +99,6 @@ public void create() {
10199
cu.addImport(resource.toString());
102100
cu.addImport(Vertx.class);
103101
cu.addImport(PostConstruct.class);
104-
cu.addImport(WebClientSession.class);
105-
cu.addImport(WebClient.class);
106102
cu.addImport(VertXRequestAdapter.class);
107103
cu.addImport(Inject.class);
108104
cu.addImport(Context.class);
@@ -130,11 +126,9 @@ public void create() {
130126
desiredMethod(clazz);
131127
fetchMethod(clazz);
132128
mapper.createPath()
133-
.map(e -> e.getValue().getPOST().getRequestBody().getContent().getMediaType("application/json"))
134-
.map(m -> m.getSchema().getRef())
135-
.map(r -> r.substring(r.lastIndexOf("/") + 1, r.length()))
136-
.ifPresent(t ->
137-
createMethod(cu, clazz, new ClassOrInterfaceType(null, t))
129+
.map(e -> e.getValue().getPOST())
130+
.ifPresent(op ->
131+
createMethod(cu, clazz, op)
138132
);
139133
mapper.patchPath()
140134
.map(e -> e.getValue().getPATCH())
@@ -219,26 +213,42 @@ private void constructor(ClassOrInterfaceDeclaration clazz) {
219213
new MethodCallExpr("super", new FieldAccessExpr(new TypeExpr(resourceType), "class"))))));
220214
}
221215

222-
private void createMethod(CompilationUnit cu, ClassOrInterfaceDeclaration clazz, Type createOptionType) {
223-
cu.addImport(resource.getQualifier().map(Name::toString).orElse("") + "." + createOptionType);
224-
cu.addImport(Creator.class);
225-
ClassOrInterfaceType creatorType = new ClassOrInterfaceType(null,
226-
new SimpleName(Creator.class.getSimpleName()),
227-
new NodeList<>(resourceType, crdType));
228-
clazz.addImplementedType(creatorType);
229-
MethodDeclaration createMethod = clazz.addMethod("create", Keyword.PUBLIC)
230-
.addAnnotation(Override.class)
231-
.addParameter(resourceType, "desired")
232-
.addParameter(crdType, "primary")
233-
.addParameter(contextType, "context")
234-
.setType(resourceType);
235-
Optional<MethodCallExpr> createCall = methodCalls.create(new NameExpr(FIELD_API_CLIENT), new NameExpr("primary"), new NodeList<>(new MethodCallExpr(new MethodCallExpr(new NameExpr("primary"), "getMetadata"),
236-
"getName")),
237-
new NodeList<>(new NameExpr("createOption")));
238-
AssignExpr assignCreateOpt = new AssignExpr(new VariableDeclarationExpr(createOptionType, "createOption"), new MethodCallExpr(null, "fromResource", new NodeList<>(new NameExpr("primary"), new MethodReferenceExpr(new TypeExpr(createOptionType), new NodeList<>(),"createFromDiscriminatorValue"))),Operator.ASSIGN);
239-
ReturnStmt createReturn = createCall
240-
.map(m -> new ReturnStmt(m)).orElse(new ReturnStmt(new NullLiteralExpr()));
241-
createMethod.setBody(new BlockStmt(new NodeList<>(new ExpressionStmt(assignCreateOpt), createReturn)));
216+
private void createMethod(CompilationUnit cu, ClassOrInterfaceDeclaration clazz, Operation op) {
217+
Schema schema = op.getRequestBody().getContent().getMediaType("application/json").getSchema();
218+
String typeName;
219+
if (schema.getRef() != null) {
220+
typeName = schema.getRef().substring(schema.getRef().lastIndexOf("/") + 1, schema.getRef().length());
221+
ClassOrInterfaceType createOptionType = new ClassOrInterfaceType(null, typeName);
222+
223+
cu.addImport(resource.getQualifier().map(Name::toString).orElse("") + "." + createOptionType);
224+
cu.addImport(Creator.class);
225+
ClassOrInterfaceType creatorType = new ClassOrInterfaceType(null,
226+
new SimpleName(Creator.class.getSimpleName()),
227+
new NodeList<>(resourceType, crdType));
228+
clazz.addImplementedType(creatorType);
229+
MethodDeclaration createMethod = clazz.addMethod("create", Keyword.PUBLIC)
230+
.addAnnotation(Override.class)
231+
.addParameter(resourceType, "desired")
232+
.addParameter(crdType, "primary")
233+
.addParameter(contextType, "context")
234+
.setType(resourceType);
235+
Optional<MethodCallExpr> createCall = methodCalls.create(new NameExpr(FIELD_API_CLIENT), new NameExpr("primary"), new NodeList<>(new MethodCallExpr(new MethodCallExpr(new NameExpr("primary"), "getMetadata"),
236+
"getName")),
237+
new NodeList<>(new NameExpr("createOption")));
238+
AssignExpr assignCreateOpt = new AssignExpr(new VariableDeclarationExpr(createOptionType, "createOption"), new MethodCallExpr(null, "fromResource", new NodeList<>(new NameExpr("primary"), new MethodReferenceExpr(new TypeExpr(createOptionType), new NodeList<>(),"createFromDiscriminatorValue"))),Operator.ASSIGN);
239+
BlockStmt body = new BlockStmt(new NodeList<>(new ExpressionStmt(assignCreateOpt)));
240+
ReturnStmt createReturn;
241+
if (op.getResponses().getAPIResponse("201") != null && op.getResponses().getAPIResponse("201").getContent() != null) {
242+
createReturn = createCall
243+
.map(m -> new ReturnStmt(m)).orElse(new ReturnStmt(new NullLiteralExpr()));
244+
} else {
245+
createCall
246+
.ifPresent(m -> body.addStatement(m));
247+
createReturn = new ReturnStmt(new NameExpr("desired"));
248+
}
249+
body.addStatement(createReturn);
250+
createMethod.setBody(body);
251+
}
242252
}
243253

244254
private void updateMethod(CompilationUnit cu, ClassOrInterfaceDeclaration clazz, Operation op) {

0 commit comments

Comments
 (0)