Skip to content

Commit e463dc4

Browse files
andreaTPmanusa
authored andcommitted
Add more test and edge cases
1 parent bcdfd56 commit e463dc4

File tree

6 files changed

+114
-4
lines changed

6 files changed

+114
-4
lines changed

crd-generator/api/src/main/java/io/fabric8/crd/generator/AbstractJsonSchema.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ protected T internalFrom(TypeDef definition, String... ignore) {
116116
}
117117

118118
private T internalFromImpl(TypeDef definition, Set<String> visited, String... ignore) {
119-
visited.add(definition.getFullyQualifiedName());
120119
final B builder = newBuilder();
121120
Set<String> ignores =
122121
ignore.length > 0 ? new LinkedHashSet<>(Arrays.asList(ignore)) : Collections
@@ -417,9 +416,11 @@ private T internalFromImpl(String name, TypeRef typeRef, Set<String> visited) {
417416
.toArray(JsonNode[]::new);
418417
return enumProperty(enumValues);
419418
} else {
420-
if (!def.getFullyQualifiedName().startsWith("java") && visited.contains(def.getFullyQualifiedName())) {
421-
throw new IllegalArgumentException("Found a cyclic reference involving " + def.getFullyQualifiedName());
419+
String visitedName = def.getFullyQualifiedName() + "-" + name;
420+
if (!def.getFullyQualifiedName().startsWith("java") && visited.contains(visitedName)) {
421+
throw new IllegalArgumentException("Found a cyclic reference involving the field " + name + " of type " + def.getFullyQualifiedName());
422422
}
423+
visited.add(visitedName);
423424
return internalFromImpl(def, visited);
424425
}
425426

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.crd.example.nocyclic;
17+
18+
import io.fabric8.kubernetes.api.model.Namespaced;
19+
import io.fabric8.kubernetes.client.CustomResource;
20+
import io.fabric8.kubernetes.model.annotation.Group;
21+
import io.fabric8.kubernetes.model.annotation.Version;
22+
23+
@Group("sample.fabric8.io")
24+
@Version("v1alpha1")
25+
public class NoCyclic extends CustomResource<NoCyclicSpec, NoCyclicStatus> implements Namespaced {
26+
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.crd.example.nocyclic;
17+
18+
public class NoCyclicSpec {
19+
private Ref ref1;
20+
private Ref ref2;
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.crd.example.nocyclic;
17+
18+
public class NoCyclicStatus {
19+
private String message;
20+
// private Ref ref1;
21+
22+
public String getMessage() {
23+
return message;
24+
}
25+
26+
public void setMessage(String message) {
27+
this.message = message;
28+
}
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.crd.example.nocyclic;
17+
18+
public class Ref {
19+
20+
private int ref;
21+
22+
}

crd-generator/api/src/test/java/io/fabric8/crd/generator/CRDGeneratorTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.fabric8.crd.example.joke.JokeRequestStatus;
2727
import io.fabric8.crd.example.multiple.v1.Multiple;
2828
import io.fabric8.crd.example.multiple.v1.MultipleSpec;
29+
import io.fabric8.crd.example.nocyclic.NoCyclic;
2930
import io.fabric8.crd.example.simplest.Simplest;
3031
import io.fabric8.crd.example.simplest.SimplestSpec;
3132
import io.fabric8.crd.example.simplest.SimplestStatus;
@@ -207,10 +208,19 @@ void shouldProperlyGenerateMultipleVersionsOfCRDs() {
207208
assertThrows(
208209
IllegalArgumentException.class,
209210
() -> generator.detailedGenerate(),
210-
"Found a cyclic reference involving io.fabric8.crd.example.cyclic.Ref"
211+
"An IllegalArgument Exception hasn't been thrown when generating a CRD with cyclic references"
211212
);
212213
}
213214

215+
@Test void notGeneratingACycleButReusingShouldSucceed() {
216+
final CRDGenerator generator = new CRDGenerator()
217+
.customResourceClasses(NoCyclic.class)
218+
.forCRDVersions("v1", "v1beta1")
219+
.withOutput(output);
220+
221+
CRDGenerationInfo info = generator.detailedGenerate();
222+
assertEquals(2, info.numberOfGeneratedCRDs());
223+
}
214224

215225
@FunctionalInterface
216226
private interface CRTest {

0 commit comments

Comments
 (0)