9
9
10
10
import org .acme .client .ApiClientMethodCallFactory ;
11
11
import org .acme .read .crud .CrudMapper ;
12
+ import org .eclipse .microprofile .openapi .models .Operation ;
12
13
13
14
import com .github .javaparser .ast .CompilationUnit ;
14
15
import com .github .javaparser .ast .Modifier .Keyword ;
@@ -120,17 +121,12 @@ public void create() {
120
121
new SimpleName (PerResourcePollingDependentResource .class .getSimpleName ()),
121
122
new NodeList <>(resourceType , crdType ));
122
123
123
- ClassOrInterfaceType updaterType = new ClassOrInterfaceType (null ,
124
- new SimpleName (Updater .class .getSimpleName ()),
125
- new NodeList <>(resourceType , crdType ));
126
- ClassOrInterfaceType deleterType = new ClassOrInterfaceType (null ,
127
- new SimpleName (Deleter .class .getSimpleName ()),
128
- new NodeList <>(crdType ));
124
+
125
+
129
126
130
127
ClassOrInterfaceDeclaration clazz = cu .addClass (className , Keyword .PUBLIC )
131
- .addExtendedType (dependentType )
132
- .addImplementedType (updaterType )
133
- .addImplementedType (deleterType );
128
+ .addExtendedType (dependentType );
129
+
134
130
135
131
fields (clazz );
136
132
constructor (clazz );
@@ -146,11 +142,9 @@ public void create() {
146
142
);
147
143
mapper .patchPath ()
148
144
.map (e -> e .getValue ().getPATCH ())
149
- .map (p -> p .getRequestBody ().getContent ().getMediaType ("application/json" ))
150
- .map (m -> m .getSchema ().getRef ())
151
- .map (r -> r .substring (r .lastIndexOf ("/" ) + 1 , r .length ()))
152
- .ifPresent (t ->
153
- updateMethod (cu , clazz , new ClassOrInterfaceType (null , t ))
145
+ .or (() -> mapper .putPath ().map (e -> e .getValue ().getPUT ()))
146
+ .ifPresent (op ->
147
+ updateMethod (cu , clazz , op )
154
148
);
155
149
deleteMethod (cu , clazz );
156
150
fromResourceMethod (clazz );
@@ -174,6 +168,10 @@ private void fetchMethod(ClassOrInterfaceDeclaration clazz) {
174
168
175
169
private void deleteMethod (CompilationUnit cu , ClassOrInterfaceDeclaration clazz ) {
176
170
cu .addImport (Deleter .class );
171
+ ClassOrInterfaceType deleterType = new ClassOrInterfaceType (null ,
172
+ new SimpleName (Deleter .class .getSimpleName ()),
173
+ new NodeList <>(crdType ));
174
+ clazz .addImplementedType (deleterType );
177
175
MethodDeclaration deleteMethod = clazz .addMethod ("delete" , Keyword .PUBLIC )
178
176
.addAnnotation (Override .class )
179
177
.addParameter (crdType , "primary" )
@@ -247,9 +245,16 @@ private void createMethod(CompilationUnit cu, ClassOrInterfaceDeclaration clazz,
247
245
createMethod .setBody (new BlockStmt (new NodeList <>(new ExpressionStmt (assignCreateOpt ), createReturn )));
248
246
}
249
247
250
- private void updateMethod (CompilationUnit cu , ClassOrInterfaceDeclaration clazz , Type updateOptionType ) {
248
+ private void updateMethod (CompilationUnit cu , ClassOrInterfaceDeclaration clazz , Operation op ) {
249
+ String ref = op .getRequestBody ().getContent ().getMediaType ("application/json" ).getSchema ().getRef ();
250
+ String typeName = ref .substring (ref .lastIndexOf ("/" ) + 1 , ref .length ());
251
+ ClassOrInterfaceType updateOptionType = new ClassOrInterfaceType (null , typeName );
251
252
cu .addImport (Updater .class );
252
253
cu .addImport (resource .getQualifier ().map (Name ::toString ).orElse ("" ) + "." + updateOptionType );
254
+ ClassOrInterfaceType updaterType = new ClassOrInterfaceType (null ,
255
+ new SimpleName (Updater .class .getSimpleName ()),
256
+ new NodeList <>(resourceType , crdType ));
257
+ clazz .addImplementedType (updaterType );
253
258
MethodDeclaration updateMethod = clazz .addMethod ("update" , Keyword .PUBLIC )
254
259
.addAnnotation (Override .class )
255
260
.addParameter (resourceType , "actual" )
@@ -262,9 +267,18 @@ private void updateMethod(CompilationUnit cu, ClassOrInterfaceDeclaration clazz,
262
267
"getName" )),
263
268
new NodeList <>(new NameExpr ("editOption" )));
264
269
AssignExpr assignUpdateOpt = new AssignExpr (new VariableDeclarationExpr (updateOptionType , "editOption" ), new MethodCallExpr (null , "fromResource" , new NodeList <>(new NameExpr ("primary" ), new MethodReferenceExpr (new TypeExpr (updateOptionType ), new NodeList <>(),"createFromDiscriminatorValue" ))),Operator .ASSIGN );
265
- ReturnStmt updateReturn = updateCall
266
- .map (m -> new ReturnStmt (m )).orElse (new ReturnStmt (new NullLiteralExpr ()));
267
- updateMethod .setBody (new BlockStmt (new NodeList <>(new ExpressionStmt (assignUpdateOpt ), updateReturn )));
270
+ BlockStmt body = new BlockStmt (new NodeList <>(new ExpressionStmt (assignUpdateOpt )));
271
+ ReturnStmt updateReturn ;
272
+ if (op .getResponses ().getAPIResponse ("200" ) != null && op .getResponses ().getAPIResponse ("200" ).getContent () != null ) {
273
+ updateReturn = updateCall
274
+ .map (m -> new ReturnStmt (m )).orElse (new ReturnStmt (new NullLiteralExpr ()));
275
+ } else {
276
+ updateCall
277
+ .ifPresent (m -> body .addStatement (m ));
278
+ updateReturn = new ReturnStmt (new NameExpr ("desired" ));
279
+ }
280
+ body .addStatement (updateReturn );
281
+ updateMethod .setBody (body );
268
282
}
269
283
270
284
private void fromResourceMethod (ClassOrInterfaceDeclaration clazz ) {
0 commit comments