Skip to content

Commit 26ab9ca

Browse files
committed
Support api specs without components.responses and put as alternative to
patch
1 parent f98d617 commit 26ab9ca

10 files changed

+591
-43
lines changed

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import org.acme.gen.CrdResourceGen;
1717
import org.acme.gen.DependentGen;
1818
import org.acme.gen.ReconcilerGen;
19-
import org.acme.read.ResponseTypeReader;
19+
import org.acme.read.ModelReader;
2020
import org.acme.read.crud.ResponseTypeMapper;
2121
import org.apache.maven.plugin.AbstractMojo;
2222
import org.apache.maven.plugin.MojoExecutionException;
@@ -98,10 +98,9 @@ public void processOpenApiFolder() {
9898

9999
private void pocessOpenApiFile(File jsonsFile) {
100100
OpenAPI openApiDoc = loadOpenApiDoc(jsonsFile, config.getConfig());
101-
ResponseTypeReader reader = new ResponseTypeReader(openApiDoc);
102-
reader.getResponseTypeNames(e -> schemas.contains(e.getKey())).forEach(r ->
103-
processResponseType(openApiDoc, jsonsFile, r)
104-
);
101+
ModelReader responseTypeReader = new ModelReader(openApiDoc);
102+
responseTypeReader.getResponseTypeOrSchemaNames(e -> schemas.contains(e.getKey()))
103+
.forEach(r -> processResponseType(openApiDoc, jsonsFile, r));
105104
}
106105

107106
private void processResponseType(OpenAPI openApiDoc, File jsonsFile, String responseType) {

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import com.github.javaparser.ast.expr.NameExpr;
1313

1414
public class KiotaMethodCallFactory implements ApiClientMethodCallFactory {
15-
1615
private final CrudMapper mapper;
1716
private final ParameterResolver resolver;
1817

@@ -40,9 +39,15 @@ public Optional<MethodCallExpr> create(NameExpr apiClient, NameExpr primary, Nod
4039
@Override
4140
public Optional<MethodCallExpr> update(NameExpr apiClient, NameExpr primary, NodeList<Expression> byIdArgs, NodeList<Expression> patchArgs) {
4241
Optional<Entry<String, PathItem>> patchPath = mapper.patchPath();
43-
return patchPath
42+
Optional<MethodCallExpr> patchPathResult = patchPath
4443
.map(Entry::getKey)
4544
.map(k -> new MethodCallExpr(toMethodCallExpression(apiClient, k, resolver.resolveArgs(k, primary, byIdArgs)), "patch", new NodeList<>(patchArgs)));
45+
if (patchPathResult.isEmpty()) {
46+
return mapper.putPath().map(Entry::getKey)
47+
.map(k -> new MethodCallExpr(toMethodCallExpression(apiClient, k, resolver.resolveArgs(k, primary, byIdArgs)), "put", new NodeList<>(patchArgs)));
48+
} else {
49+
return patchPathResult;
50+
}
4651
}
4752

4853
@Override

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void create() {
110110
}
111111

112112
private void mapProperties(JsonNode jsonNodeTree, JSONSchemaPropsBuilder specBuilder, JSONSchemaPropsBuilder statusBuilder) {
113-
mapper.getCreateSchema().ifPresent(crtSch -> mapper.getUpdateSchema().ifPresent(uptSch -> {
113+
mapper.getCreateSchema().ifPresent(crtSch -> mapper.getPatchSchema().ifPresent(uptSch -> {
114114
JsonNode crtSchema = jsonNodeTree.at(removeLeadingHash(crtSch.getRef()));
115115
JsonNode uptSchema = jsonNodeTree.at(removeLeadingHash(uptSch.getRef()));
116116
Set<Entry<String, JsonNode>> unionOfFields = unionOfFields(crtSchema.get("properties"), uptSchema.get("properties"));

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

+32-18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import org.acme.client.ApiClientMethodCallFactory;
1111
import org.acme.read.crud.CrudMapper;
12+
import org.eclipse.microprofile.openapi.models.Operation;
1213

1314
import com.github.javaparser.ast.CompilationUnit;
1415
import com.github.javaparser.ast.Modifier.Keyword;
@@ -120,17 +121,12 @@ public void create() {
120121
new SimpleName(PerResourcePollingDependentResource.class.getSimpleName()),
121122
new NodeList<>(resourceType, crdType));
122123

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+
129126

130127
ClassOrInterfaceDeclaration clazz = cu.addClass(className, Keyword.PUBLIC)
131-
.addExtendedType(dependentType)
132-
.addImplementedType(updaterType)
133-
.addImplementedType(deleterType);
128+
.addExtendedType(dependentType);
129+
134130

135131
fields(clazz);
136132
constructor(clazz);
@@ -146,11 +142,9 @@ public void create() {
146142
);
147143
mapper.patchPath()
148144
.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)
154148
);
155149
deleteMethod(cu, clazz);
156150
fromResourceMethod(clazz);
@@ -174,6 +168,10 @@ private void fetchMethod(ClassOrInterfaceDeclaration clazz) {
174168

175169
private void deleteMethod(CompilationUnit cu, ClassOrInterfaceDeclaration clazz) {
176170
cu.addImport(Deleter.class);
171+
ClassOrInterfaceType deleterType = new ClassOrInterfaceType(null,
172+
new SimpleName(Deleter.class.getSimpleName()),
173+
new NodeList<>(crdType));
174+
clazz.addImplementedType(deleterType);
177175
MethodDeclaration deleteMethod = clazz.addMethod("delete", Keyword.PUBLIC)
178176
.addAnnotation(Override.class)
179177
.addParameter(crdType, "primary")
@@ -247,9 +245,16 @@ private void createMethod(CompilationUnit cu, ClassOrInterfaceDeclaration clazz,
247245
createMethod.setBody(new BlockStmt(new NodeList<>(new ExpressionStmt(assignCreateOpt), createReturn)));
248246
}
249247

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);
251252
cu.addImport(Updater.class);
252253
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);
253258
MethodDeclaration updateMethod = clazz.addMethod("update", Keyword.PUBLIC)
254259
.addAnnotation(Override.class)
255260
.addParameter(resourceType, "actual")
@@ -262,9 +267,18 @@ private void updateMethod(CompilationUnit cu, ClassOrInterfaceDeclaration clazz,
262267
"getName")),
263268
new NodeList<>(new NameExpr("editOption")));
264269
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);
268282
}
269283

270284
private void fromResourceMethod(ClassOrInterfaceDeclaration clazz) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.acme.read;
2+
3+
import java.util.Map.Entry;
4+
import java.util.function.Predicate;
5+
import java.util.stream.Stream;
6+
7+
import org.eclipse.microprofile.openapi.models.OpenAPI;
8+
9+
public class ModelReader {
10+
11+
private final OpenAPI api;
12+
13+
public ModelReader(OpenAPI api) {
14+
super();
15+
this.api = api;
16+
}
17+
18+
public Stream<String> getResponseTypeOrSchemaNames(Predicate<Entry<String, ?>> filter) {
19+
if (api.getComponents().getResponses() != null && !api.getComponents().getResponses().isEmpty()) {
20+
return getResponseTypeNames(filter);
21+
} else {
22+
return getSchemaNames(filter);
23+
}
24+
}
25+
26+
public Stream<String> getResponseTypeNames() {
27+
return getResponseTypeNames(e -> true);
28+
}
29+
30+
public Stream<String> getSchemaNames() {
31+
return getSchemaNames(e -> true);
32+
}
33+
34+
private Stream<String> getResponseTypeNames(Predicate<Entry<String, ?>> filter) {
35+
if (api.getComponents().getResponses() == null) {
36+
return Stream.empty();
37+
} else {
38+
return api.getComponents().getResponses().entrySet().stream().filter(filter).map(Entry::getKey);
39+
}
40+
41+
}
42+
43+
private Stream<String> getSchemaNames(Predicate<Entry<String, ?>> filter) {
44+
if (api.getComponents().getSchemas() == null) {
45+
return Stream.empty();
46+
} else {
47+
return api.getComponents().getSchemas().entrySet().stream().filter(filter).map(Entry::getKey);
48+
}
49+
}
50+
51+
}

operator-gen-maven-plugin/src/main/java/org/acme/read/crud/CrudMapper.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ public interface CrudMapper {
1616

1717
Optional<Entry<String, PathItem>> patchPath();
1818

19+
Optional<Entry<String, PathItem>> putPath();
20+
1921
Schema getByIdSchema();
2022

2123
Optional<Schema> getCreateSchema();
2224

23-
Optional<Schema> getUpdateSchema();
25+
Optional<Schema> getPatchSchema();
26+
27+
Optional<Schema> getPutSchema();
28+
2429

2530
}

0 commit comments

Comments
 (0)