Skip to content

Commit 80c9b57

Browse files
committed
Support different path param types for #7 What field from crd should be
used as an identifier for the resource
1 parent 3322faf commit 80c9b57

File tree

4 files changed

+49
-17
lines changed

4 files changed

+49
-17
lines changed

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

+1-5
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,6 @@ public class OperatorGenMojo
7474
private Configuration config;
7575

7676

77-
public OperatorGenMojo() {
78-
79-
}
80-
8177
public void execute()
8278
throws MojoExecutionException
8379
{
@@ -112,7 +108,7 @@ private void processResponseType(OpenAPI openApiDoc, File jsonsFile, String resp
112108
String crdVersion = config.getCrdVersion();
113109
String basePackage = config.getCrdPackage();
114110
ResponseTypeMapper mapper = new ResponseTypeMapper(openApiDoc, responseType);
115-
ParameterResolver resolver = new ParameterResolver(config);
111+
ParameterResolver resolver = new ParameterResolver(config, openApiDoc);
116112
ApiClientMethodCallFactory methodCalls = new KiotaMethodCallFactory(mapper, resolver);
117113
String className = responseType.substring(0, 1).toUpperCase() + responseType.substring(1);
118114
Name crdName = new Name(new Name(basePackage), className);

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
import java.util.Arrays;
44
import java.util.List;
55
import java.util.Map;
6+
import java.util.Optional;
67
import java.util.Set;
78
import java.util.function.Predicate;
89
import java.util.stream.Stream;
910

1011
import org.acme.Configuration;
12+
import org.eclipse.microprofile.openapi.models.OpenAPI;
13+
import org.eclipse.microprofile.openapi.models.Operation;
14+
import org.eclipse.microprofile.openapi.models.PathItem;
15+
import org.eclipse.microprofile.openapi.models.parameters.Parameter;
1116

1217
import com.github.javaparser.ast.NodeList;
1318
import com.github.javaparser.ast.expr.Expression;
@@ -17,9 +22,19 @@
1722
public class ParameterResolver {
1823

1924
private final Configuration config;
25+
private final OpenAPI openApiDoc;
2026

21-
public ParameterResolver(Configuration config) {
27+
public ParameterResolver(Configuration config, OpenAPI openApiDoc) {
2228
this.config = config;
29+
this.openApiDoc = openApiDoc;
30+
}
31+
32+
public Optional<Parameter> getParameterType(String path, int paramIndex) {
33+
PathItem pathItem = openApiDoc.getPaths().getPathItem(path);
34+
Optional<Operation> oneOfIdOps = Optional.ofNullable(pathItem.getGET())
35+
.or(() -> Optional.ofNullable(pathItem.getPATCH()))
36+
.or(() -> Optional.ofNullable(pathItem.getDELETE()));
37+
return oneOfIdOps.map(o -> o.getParameters().get(paramIndex));
2338
}
2439

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

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

+31-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.nio.file.Files;
66
import java.nio.file.Path;
77
import java.nio.file.StandardOpenOption;
8+
import java.util.ArrayList;
89
import java.util.Arrays;
910
import java.util.Collection;
1011
import java.util.Collections;
@@ -46,6 +47,17 @@
4647
* patch request schema will be moved to the CRD status.
4748
*/
4849
public class CrdResourceGen {
50+
51+
private static class FieldNameType {
52+
String name;
53+
String type;
54+
FieldNameType(String name, String type) {
55+
this.name = name;
56+
this.type = type;
57+
}
58+
}
59+
60+
4961
private static final Logger LOG = LoggerFactory.getLogger(CrdResourceGen.class);
5062
private static final String NODE_TEMPLATE = "{\"type\": \"%s\"}";
5163
private final Path path;
@@ -55,6 +67,8 @@ public class CrdResourceGen {
5567
private final ParameterResolver resolver;
5668
private final ObjectMapper objMapper = new ObjectMapper();
5769

70+
71+
5872

5973
public CrdResourceGen(Path path, Path openApiJson, Name name, CrudMapper mapper, ParameterResolver resolver) {
6074
super();
@@ -112,24 +126,31 @@ private void mapProperties(JsonNode jsonNodeTree, JSONSchemaPropsBuilder specBui
112126
private void addMissingFieldsFromPathParamMappings(Set<Entry<String, JsonNode>> fields, String prefix) {
113127
resolver.getPathParamMappingKeys().stream()
114128
.filter(this::oneOfCrudPathsMatches)
115-
//TODO Determine the type of the param
116-
// Use the path to determine the params and add info to param 1,2,3
117-
.flatMap(s -> resolver.paramMappingValueList(resolver.getPathParamMappings().get(s)).stream())
118-
.filter(v -> v.startsWith(prefix + "."))
129+
.flatMap(s -> {
130+
List<String> paramMappingValueList = resolver.paramMappingValueList(resolver.getPathParamMappings().get(s));
131+
List<FieldNameType> fieldEntries = new ArrayList<>();
132+
for (int i = 0; i < paramMappingValueList.size(); i++) {
133+
fieldEntries.add(new FieldNameType(paramMappingValueList.get(i), resolver.getParameterType(s, i)
134+
.map(p -> p.getSchema().getType().toString()).orElse("string")));
135+
}
136+
return fieldEntries.stream();
137+
})
138+
.filter(v -> v.name.startsWith(prefix + "."))
119139
.map(v -> removePrefix(prefix, v))
120-
.filter(v -> fields.stream().noneMatch(e -> e.getKey().equals(v)))
140+
.filter(v -> fields.stream().noneMatch(e -> e.getKey().equals(v.name)))
121141
.forEach(v -> {
122142
try {
123-
124-
fields.add(Map.entry(v, objMapper.readTree(String.format(NODE_TEMPLATE, "string"))));
143+
System.out.println(v.name);
144+
fields.add(Map.entry(v.name, objMapper.readTree(String.format(NODE_TEMPLATE, v.type))));
125145
} catch (JsonProcessingException e) {
126146
LOG.error("Error adding JSON node", e);
127147
}
128-
});
148+
});
129149
}
130150

131-
private String removePrefix(String prefix, String v) {
132-
return v.substring(prefix.length() + 1);
151+
private FieldNameType removePrefix(String prefix, FieldNameType fieldNameType) {
152+
fieldNameType.name = fieldNameType.name.substring(prefix.length() + 1);
153+
return fieldNameType;
133154
}
134155

135156
private boolean oneOfCrudPathsMatches(String s) {

operator-gen-maven-plugin/src/test/java/org/acme/client/KiotaClientNamingTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public V setValue(V value) {
5656
@BeforeEach
5757
void setUp() {
5858
mapper = Mockito.mock(CrudMapper.class);
59-
client = new KiotaMethodCallFactory(mapper, new ParameterResolver(new Configuration(ConfigProvider.getConfig(), new ArrayList<>())));
59+
client = new KiotaMethodCallFactory(mapper, new ParameterResolver(new Configuration(ConfigProvider.getConfig(), new ArrayList<>()), null));
6060
}
6161

6262
@Test

0 commit comments

Comments
 (0)