Skip to content

Commit 7db85f5

Browse files
committed
Support put schema for CRD generation
1 parent c65b467 commit 7db85f5

File tree

5 files changed

+16515
-26
lines changed

5 files changed

+16515
-26
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.acme.gen.DependentGen;
1818
import org.acme.gen.ReconcilerGen;
1919
import org.acme.read.ModelReader;
20+
import org.acme.read.crud.CrudMapper;
2021
import org.acme.read.crud.ResponseTypeMapper;
2122
import org.apache.maven.plugin.AbstractMojo;
2223
import org.apache.maven.plugin.MojoExecutionException;
@@ -106,7 +107,7 @@ private void pocessOpenApiFile(File jsonsFile) {
106107
private void processResponseType(OpenAPI openApiDoc, File jsonsFile, String responseType) {
107108
String crdVersion = config.getCrdVersion();
108109
String basePackage = config.getCrdPackage();
109-
ResponseTypeMapper mapper = new ResponseTypeMapper(openApiDoc, responseType);
110+
CrudMapper mapper = new ResponseTypeMapper(openApiDoc, responseType);
110111
ParameterResolver resolver = new ParameterResolver(config, openApiDoc);
111112
ApiClientMethodCallFactory methodCalls = new KiotaMethodCallFactory(mapper, resolver);
112113
String className = responseType.substring(0, 1).toUpperCase() + responseType.substring(1);

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

+53-24
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.LinkedHashSet;
1414
import java.util.List;
1515
import java.util.Map;
16+
import java.util.Optional;
1617
import java.util.Map.Entry;
1718
import java.util.Set;
1819
import java.util.stream.Collectors;
@@ -48,6 +49,10 @@
4849
*/
4950
public class CrdResourceGen {
5051

52+
private static final String ATTR_PROPS = "properties";
53+
54+
55+
5156
private static class FieldNameType {
5257
String name;
5358
String type;
@@ -110,19 +115,29 @@ public void create() {
110115
}
111116

112117
private void mapProperties(JsonNode jsonNodeTree, JSONSchemaPropsBuilder specBuilder, JSONSchemaPropsBuilder statusBuilder) {
113-
mapper.getCreateSchema().ifPresent(crtSch -> mapper.getPatchSchema().ifPresent(uptSch -> {
114-
JsonNode crtSchema = jsonNodeTree.at(removeLeadingHash(crtSch.getRef()));
115-
JsonNode uptSchema = jsonNodeTree.at(removeLeadingHash(uptSch.getRef()));
116-
Set<Entry<String, JsonNode>> unionOfFields = unionOfFields(crtSchema.get("properties"), uptSchema.get("properties"));
118+
Set<Entry<String, JsonNode>> allSpecFields = mapper.getCreateSchema().flatMap(crtSch -> mapper.getPatchSchema().or(mapper::getPutSchema).map(uptSch -> {
119+
JsonNode crtSchemaProps = Optional.ofNullable(crtSch.getRef())
120+
.map(s -> jsonNodeTree.at(removeLeadingHash(s)))
121+
.map(n -> n.get(ATTR_PROPS)).orElse(null);
122+
JsonNode uptSchemaProps = Optional.ofNullable(uptSch.getRef())
123+
.map(s -> jsonNodeTree.at(removeLeadingHash(s)))
124+
.map(n -> n.get(ATTR_PROPS)).orElse(null);
125+
Set<Entry<String, JsonNode>> unionOfFields = unionOfFields(crtSchemaProps, uptSchemaProps);
117126
addMissingFieldsFromPathParamMappings(unionOfFields, "spec");
118127
mapProperties(specBuilder, unionOfFields, jsonNodeTree);
119-
Set<Entry<String, JsonNode>> fieldsOfNotIn = fieldsOfNotIn(jsonNodeTree.at(removeLeadingHash(mapper.getByIdSchema().getRef())).get("properties"), unionOfFields.stream().map(Entry::getKey).toList());
120-
addMissingFieldsFromPathParamMappings(fieldsOfNotIn, "status");
121-
mapProperties(statusBuilder, fieldsOfNotIn, jsonNodeTree);
122-
123-
}));
128+
return unionOfFields;
129+
})).orElse(Collections.emptySet());
130+
131+
JsonNode getSchemaProps = Optional.ofNullable(mapper.getByIdSchema().getRef())
132+
.map(s -> jsonNodeTree.at(removeLeadingHash(s)))
133+
.map(n -> n.get(ATTR_PROPS)).orElse(null);
134+
Set<Entry<String, JsonNode>> fieldsOfNotIn = fieldsOfNotIn(getSchemaProps, allSpecFields.stream().map(Entry::getKey).toList());
135+
addMissingFieldsFromPathParamMappings(fieldsOfNotIn, "status");
136+
mapProperties(statusBuilder, fieldsOfNotIn, jsonNodeTree);
124137
}
125138

139+
140+
126141
private void addMissingFieldsFromPathParamMappings(Set<Entry<String, JsonNode>> fields, String prefix) {
127142
resolver.getPathParamMappingKeys().stream()
128143
.filter(this::oneOfCrudPathsMatches)
@@ -156,6 +171,7 @@ private boolean oneOfCrudPathsMatches(String s) {
156171
return mapper.getByIdPath().filter(p -> s.equals(p.getKey())).isPresent()
157172
|| mapper.createPath().filter(p -> s.equals(p.getKey())).isPresent()
158173
|| mapper.patchPath().filter(p -> s.equals(p.getKey())).isPresent()
174+
|| mapper.putPath().filter(p -> s.equals(p.getKey())).isPresent()
159175
|| mapper.deletePath().filter(p -> s.equals(p.getKey())).isPresent();
160176
}
161177

@@ -173,24 +189,29 @@ private void mapProperties(JSONSchemaPropsBuilder builder, Set<Entry<String, Jso
173189

174190
private void mapProperties(JSONSchemaPropsBuilder builder, Set<Entry<String, JsonNode>> fields, JsonNode jsonNodeTree, Set<JsonNode> visitedNodes) {
175191
mapPrimitiveTypeProps(builder, fields);
176-
mapArrayTypeProps(builder, fields, jsonNodeTree);
177-
mapObjectTypeProps(builder, jsonNodeTree, visitedNodes, fields);
192+
mapArrayTypeProps(builder, fields, jsonNodeTree, visitedNodes);
193+
mapObjectTypeProps(builder, fields, jsonNodeTree, visitedNodes);
178194
}
179195

180-
private void mapArrayTypeProps(JSONSchemaPropsBuilder builder, Set<Entry<String, JsonNode>> fields, JsonNode jsonNodeTree) {
196+
private void mapArrayTypeProps(JSONSchemaPropsBuilder builder, Set<Entry<String, JsonNode>> fields, JsonNode jsonNodeTree, Set<JsonNode> visitedNodes) {
181197
fields.stream()
182198
.filter(f -> f.getValue().get("type") != null)
183199
.filter(f -> "array".equals(f.getValue().get("type").asText()))
184200
.filter(f -> f.getValue().get("items") != null)
185201
.forEach(f -> {
202+
LOG.info("Array {} ",f.getKey());
186203
JSONSchemaPropsBuilder jsonSchemaPropsBuilder = new JSONSchemaPropsBuilder();
187204
JSONSchemaPropsBuilder itemPropsBuilder = new JSONSchemaPropsBuilder();
188205
if (f.getValue().get("items").get("type") != null) {
189206
itemPropsBuilder.withType(f.getValue().get("items").get("type").asText());
190207
}
191208
if (f.getValue().get("items").get("$ref") != null) {
192209
JsonNode schema = jsonNodeTree.at(removeLeadingHash(f.getValue().get("items").get("$ref").asText()));
193-
mapProperties(itemPropsBuilder, fields(schema.get("properties")), jsonNodeTree, new HashSet<>());
210+
LOG.info("Schema {} ", f.getValue().get("items").get("$ref"));
211+
if(!visitedNodes.contains(schema)) {
212+
visitedNodes.add(schema);
213+
mapProperties(itemPropsBuilder, fields(schema.get(ATTR_PROPS)), jsonNodeTree, visitedNodes);
214+
}
194215
itemPropsBuilder.withType("object");
195216
}
196217
builder.addToProperties(f.getKey(),
@@ -206,16 +227,16 @@ private void mapPrimitiveTypeProps(JSONSchemaPropsBuilder builder, Set<Entry<Str
206227
new JSONSchemaPropsBuilder().withType(f.getValue().get("type").asText()).build()));
207228
}
208229

209-
private void mapObjectTypeProps(JSONSchemaPropsBuilder builder, JsonNode jsonNodeTree,
210-
Set<JsonNode> visitedNodes, Set<Entry<String, JsonNode>> fields) {
230+
private void mapObjectTypeProps(JSONSchemaPropsBuilder builder, Set<Entry<String, JsonNode>> fields, JsonNode jsonNodeTree,
231+
Set<JsonNode> visitedNodes) {
211232
fields.stream()
212233
.filter(f -> f.getValue().get("$ref") != null)
213234
.forEach(f -> {
214235
JsonNode schema = jsonNodeTree.at(removeLeadingHash(f.getValue().get("$ref").asText()));
215236
if(!visitedNodes.contains(schema)) {
216237
visitedNodes.add(schema);
217238
JSONSchemaPropsBuilder objectTypeBuilder = new JSONSchemaPropsBuilder();
218-
mapProperties(objectTypeBuilder, fields(schema.get("properties")), jsonNodeTree, visitedNodes);
239+
mapProperties(objectTypeBuilder, fields(schema.get(ATTR_PROPS)), jsonNodeTree, visitedNodes);
219240
builder.addToProperties(f.getKey(),
220241
objectTypeBuilder.withType("object").build());
221242
}
@@ -224,24 +245,32 @@ private void mapObjectTypeProps(JSONSchemaPropsBuilder builder, JsonNode jsonNod
224245

225246
private Set<Entry<String, JsonNode>> unionOfFields(JsonNode a, JsonNode b) {
226247
Set<Entry<String, JsonNode>> union = new LinkedHashSet<>();
227-
a.fields().forEachRemaining(union::add);
228-
b.fields().forEachRemaining(union::add);
248+
if (a != null) {
249+
a.fields().forEachRemaining(union::add);
250+
}
251+
if (b != null) {
252+
b.fields().forEachRemaining(union::add);
253+
}
229254
return union;
230255
}
231256

232257
private Set<Entry<String, JsonNode>> fields(JsonNode a) {
233258
Set<Entry<String, JsonNode>> fields = new LinkedHashSet<>();
234-
a.fields().forEachRemaining(fields::add);
259+
if (a != null) {
260+
a.fields().forEachRemaining(fields::add);
261+
}
235262
return fields;
236263
}
237264

238265
private Set<Entry<String, JsonNode>> fieldsOfNotIn(JsonNode a, Collection<String> b) {
239266
Set<Entry<String, JsonNode>> exclusiveSet = new LinkedHashSet<>();
240-
a.fields().forEachRemaining(e -> {
241-
if (!b.contains(e.getKey())) {
242-
exclusiveSet.add(e);
243-
}
244-
});
267+
if (a != null) {
268+
a.fields().forEachRemaining(e -> {
269+
if (!b.contains(e.getKey())) {
270+
exclusiveSet.add(e);
271+
}
272+
});
273+
}
245274
return exclusiveSet;
246275
}
247276

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

+2
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@ public interface CrudMapper {
2626

2727
Optional<Schema> getPutSchema();
2828

29+
Optional<Schema> resolveRef(Schema proxy);
30+
2931

3032
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ public static String resolveSchemaName(Schema schema) {
9595

9696
}
9797

98-
private Optional<Schema> resolveRef(Schema proxy) {
98+
@Override
99+
public Optional<Schema> resolveRef(Schema proxy) {
99100
if (StringUtil.isNullOrEmpty(proxy.getRef())){
100101
return Optional.of(proxy);
101102
} else {

0 commit comments

Comments
 (0)