Skip to content

Commit

Permalink
fix(java-gen): handle multiple colliding enums (#5853)
Browse files Browse the repository at this point in the history
* [java-gen] Handle multiple colliding enums

* CHANGELOG.md

* sonar

* minor
  • Loading branch information
andreaTP authored Apr 8, 2024
1 parent 8545034 commit a400d76
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### 6.12-SNAPSHOT

#### Bugs
* Fix #5853: [java-generator] Gracefully handle colliding enum definitions

#### Improvements

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -96,16 +97,27 @@ public GeneratorResult generateJava() {
.setBody(new BlockStmt().addStatement(new ReturnStmt(VALUE)));
getValue.addAnnotation("com.fasterxml.jackson.annotation.JsonValue");

for (String k : this.values) {
String constantName;
Set<String> constantNames = new HashSet<>(values.size());
for (String k : values) {
StringBuilder constantNameBuilder = new StringBuilder();
try {
// If the value can be parsed as an Integer
Integer.valueOf(k);
// Prepend
constantName = "V_" + sanitizeEnumEntry(sanitizeString(k));
constantNameBuilder.append("V_" + sanitizeEnumEntry(sanitizeString(k)));
} catch (Exception e) {
constantName = sanitizeEnumEntry(sanitizeString(k));
constantNameBuilder.append(sanitizeEnumEntry(sanitizeString(k)));
}
// enums with colliding names are bad practice, we should make sure that the resulting code compiles,
// but we don't need fancy heuristics for the naming let's just prepend an underscore until it works
while (constantNames.contains(constantNameBuilder.toString())) {
String tmp = constantNameBuilder.toString();
constantNameBuilder.setLength(0);
constantNameBuilder.append("_" + tmp);
}
String constantName = constantNameBuilder.toString();
constantNames.add(constantName);

String originalName = AbstractJSONSchema2Pojo.escapeQuotes(k);
Expression valueArgument = new StringLiteralExpr(originalName);
if (!underlyingType.equals(JAVA_LANG_STRING)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ private static Stream<Arguments> compilationTestData() {
Arguments.of("two-crds.yml", 6),
Arguments.of("folder", 6),
Arguments.of("calico-ippool-crd.yml", 3),
Arguments.of("emissary-crds.yaml", 242));
Arguments.of("emissary-crds.yaml", 242),
Arguments.of("colliding-enums-crd.yml", 2));
}

@ParameterizedTest(name = "{0} should generate {1} source files and compile OK")
Expand Down
48 changes: 48 additions & 0 deletions java-generator/core/src/test/resources/colliding-enums-crd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#
# Copyright (C) 2015 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: collidingenums.example.com
spec:
group: example.com
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
properties:
myenum:
items:
enum:
- one
- One
- onE
- OnE
- oNe
type: string
type: array
type: object
scope: Namespaced
names:
plural: collidingenums
singular: collidingenum
kind: Collidingenum
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ void testDeserialization() {
List<CertificateRequestSpec.Usages> usagesList = sample.getSpec().getUsages();

// Assert
assertEquals(5, usagesList.size());
assertEquals(7, usagesList.size());
assertEquals(CertificateRequestSpec.Usages._EMPTY, usagesList.get(0));
assertEquals(CertificateRequestSpec.Usages.SIGNING, usagesList.get(1));
assertEquals(CertificateRequestSpec.Usages.DIGITAL_SIGNATURE, usagesList.get(2));
assertEquals(CertificateRequestSpec.Usages.SERVER_AUTH, usagesList.get(3));
assertEquals(CertificateRequestSpec.Usages.S_MIME, usagesList.get(4));
assertEquals(CertificateRequestSpec.Usages.ANOTHER, usagesList.get(5));
assertEquals(CertificateRequestSpec.Usages.__ANOTHER, usagesList.get(6));

assertEquals(datetimeValue, sample.getSpec().getDatetime());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ spec:
- ocsp signing
- microsoft sgc
- netscape sgc
- another
- Another
- anotheR
username:
description: Username contains the name of the user that created the CertificateRequest. Populated by the cert-manager webhook on creation and immutable.
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ spec:
- digital signature
- server auth
- s/mime
- another
- anotheR
duration: 2160h
issuerRef:
name: ca-issuer
Expand Down

0 comments on commit a400d76

Please sign in to comment.