|
10 | 10 | import org.acme.client.ApiClientMethodCallFactory;
|
11 | 11 | import org.acme.read.crud.CrudMapper;
|
12 | 12 | import org.eclipse.microprofile.openapi.models.Operation;
|
| 13 | +import org.eclipse.microprofile.openapi.models.media.Schema; |
13 | 14 |
|
14 | 15 | import com.github.javaparser.ast.CompilationUnit;
|
15 | 16 | import com.github.javaparser.ast.Modifier.Keyword;
|
|
41 | 42 | import com.github.javaparser.ast.stmt.ThrowStmt;
|
42 | 43 | import com.github.javaparser.ast.stmt.TryStmt;
|
43 | 44 | import com.github.javaparser.ast.type.ClassOrInterfaceType;
|
44 |
| -import com.github.javaparser.ast.type.Type; |
45 | 45 | import com.github.javaparser.ast.type.TypeParameter;
|
46 | 46 | import com.github.javaparser.utils.SourceRoot;
|
47 | 47 | import com.microsoft.kiota.ApiException;
|
|
58 | 58 | import io.kiota.http.vertx.VertXRequestAdapter;
|
59 | 59 | import io.kiota.serialization.json.JsonParseNodeFactory;
|
60 | 60 | import io.vertx.core.Vertx;
|
61 |
| -import io.vertx.ext.web.client.WebClient; |
62 |
| -import io.vertx.ext.web.client.WebClientSession; |
63 | 61 | import jakarta.annotation.PostConstruct;
|
64 | 62 | import jakarta.inject.Inject;
|
65 | 63 |
|
@@ -101,8 +99,6 @@ public void create() {
|
101 | 99 | cu.addImport(resource.toString());
|
102 | 100 | cu.addImport(Vertx.class);
|
103 | 101 | cu.addImport(PostConstruct.class);
|
104 |
| - cu.addImport(WebClientSession.class); |
105 |
| - cu.addImport(WebClient.class); |
106 | 102 | cu.addImport(VertXRequestAdapter.class);
|
107 | 103 | cu.addImport(Inject.class);
|
108 | 104 | cu.addImport(Context.class);
|
@@ -130,11 +126,9 @@ public void create() {
|
130 | 126 | desiredMethod(clazz);
|
131 | 127 | fetchMethod(clazz);
|
132 | 128 | 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) |
138 | 132 | );
|
139 | 133 | mapper.patchPath()
|
140 | 134 | .map(e -> e.getValue().getPATCH())
|
@@ -219,26 +213,42 @@ private void constructor(ClassOrInterfaceDeclaration clazz) {
|
219 | 213 | new MethodCallExpr("super", new FieldAccessExpr(new TypeExpr(resourceType), "class"))))));
|
220 | 214 | }
|
221 | 215 |
|
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 | + } |
242 | 252 | }
|
243 | 253 |
|
244 | 254 | private void updateMethod(CompilationUnit cu, ClassOrInterfaceDeclaration clazz, Operation op) {
|
|
0 commit comments