From b9d6954a433f5435a2cecf5a645fc80f809ec0b1 Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Mon, 16 Jan 2023 10:47:48 +0000 Subject: [PATCH] [java-generator] Fix encoding for empty strings in enums --- CHANGELOG.md | 1 + .../io/fabric8/java/generator/nodes/JEnum.java | 15 ++++++++++++++- ...d_shouldGeneratePojos.testJokeCrd.approved.txt | 15 +++++++++++++++ .../it/certmanager/TestEnumSerialization.java | 11 ++++++----- .../test/resources/cert-manager.crds.1.7.1.yaml | 1 + .../src/test/resources/sample1.yaml | 3 ++- 6 files changed, 39 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3c01552c71..2033aecc091 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ * Fix #4574: fromServer has been deprecated - it no longer needs to be called. All get() operations will fetch the resource(s) from the api server. If you need the context item that was passed in from a resource, load, or resourceList methods, use the item or items method. * Fix #4633: client.run().withRunConfig was deprecated. Use withNewRunConfig instead. * Fix #4663: Config.maxConcurrentRequests and Config.maxConcurrentRequestsPerHost will no longer be used. Instead they will default to unlimited for all clients. Due to the ability of the fabric8 client to start long running requests (either websocket or regular http) and how this is treated by the underlying clients you can easily exhaust these values and enter a state where the client is unresponsive without any additional information on what is occurring. +* Fix #4769: [java-generator] Fix encoding of empty strings as valid enums ### 6.3.1 (2022-12-15) diff --git a/java-generator/core/src/main/java/io/fabric8/java/generator/nodes/JEnum.java b/java-generator/core/src/main/java/io/fabric8/java/generator/nodes/JEnum.java index 1e50d7de9c1..d2f8d086f75 100644 --- a/java-generator/core/src/main/java/io/fabric8/java/generator/nodes/JEnum.java +++ b/java-generator/core/src/main/java/io/fabric8/java/generator/nodes/JEnum.java @@ -17,9 +17,11 @@ import com.fasterxml.jackson.databind.JsonNode; import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.Modifier; import com.github.javaparser.ast.body.*; import com.github.javaparser.ast.expr.*; import com.github.javaparser.ast.stmt.BlockStmt; +import com.github.javaparser.ast.stmt.ReturnStmt; import io.fabric8.java.generator.Config; import java.util.ArrayList; @@ -56,7 +58,11 @@ private String sanitizeEnumEntry(final String str) { if (config.isUppercaseEnums()) { ret = ret.toUpperCase(Locale.ROOT); } - return ret.replaceAll("[\\s/]", "_"); + if (ret.isEmpty()) { + return "_EMPTY"; + } else { + return ret.replaceAll("[\\s/]", "_"); + } } @Override @@ -77,6 +83,13 @@ public GeneratorResult generateJava() { new NameExpr(VALUE), AssignExpr.Operator.ASSIGN))); + MethodDeclaration getValue = en + .addMethod("getValue", Modifier.Keyword.PUBLIC); + getValue.setType(JAVA_LANG_STRING); + getValue + .setBody(new BlockStmt().addStatement(new ReturnStmt(VALUE))); + getValue.addAnnotation("com.fasterxml.jackson.annotation.JsonValue"); + for (String k : this.values) { String constantName; try { diff --git a/java-generator/core/src/test/resources/io/fabric8/java/generator/approvals/ApprovalTest.generate_withValidCrd_shouldGeneratePojos.testJokeCrd.approved.txt b/java-generator/core/src/test/resources/io/fabric8/java/generator/approvals/ApprovalTest.generate_withValidCrd_shouldGeneratePojos.testJokeCrd.approved.txt index 212293c1cff..0924aa1828a 100644 --- a/java-generator/core/src/test/resources/io/fabric8/java/generator/approvals/ApprovalTest.generate_withValidCrd_shouldGeneratePojos.testJokeCrd.approved.txt +++ b/java-generator/core/src/test/resources/io/fabric8/java/generator/approvals/ApprovalTest.generate_withValidCrd_shouldGeneratePojos.testJokeCrd.approved.txt @@ -38,6 +38,11 @@ public class JokeRequestSpec implements io.fabric8.kubernetes.api.model.Kubernet Category(java.lang.String value) { this.value = value; } + + @com.fasterxml.jackson.annotation.JsonValue() + public java.lang.String getValue() { + return value; + } } @com.fasterxml.jackson.annotation.JsonProperty("category") @@ -72,6 +77,11 @@ public class JokeRequestSpec implements io.fabric8.kubernetes.api.model.Kubernet Excluded(java.lang.String value) { this.value = value; } + + @com.fasterxml.jackson.annotation.JsonValue() + public java.lang.String getValue() { + return value; + } } @com.fasterxml.jackson.annotation.JsonProperty("excluded") @@ -145,6 +155,11 @@ public class JokeRequestStatus implements io.fabric8.kubernetes.api.model.Kubern State(java.lang.String value) { this.value = value; } + + @com.fasterxml.jackson.annotation.JsonValue() + public java.lang.String getValue() { + return value; + } } @com.fasterxml.jackson.annotation.JsonProperty("state") diff --git a/java-generator/it/src/it/enum-ser-deser/src/test/java/io/fabric8/it/certmanager/TestEnumSerialization.java b/java-generator/it/src/it/enum-ser-deser/src/test/java/io/fabric8/it/certmanager/TestEnumSerialization.java index f34ece2b757..79b711bf231 100644 --- a/java-generator/it/src/it/enum-ser-deser/src/test/java/io/fabric8/it/certmanager/TestEnumSerialization.java +++ b/java-generator/it/src/it/enum-ser-deser/src/test/java/io/fabric8/it/certmanager/TestEnumSerialization.java @@ -41,11 +41,12 @@ void testDeserialization() { List usagesList = sample.getSpec().getUsages(); // Assert - assertEquals(4, usagesList.size()); - assertEquals(CertificateRequestSpec.Usages.SIGNING, usagesList.get(0)); - assertEquals(CertificateRequestSpec.Usages.DIGITAL_SIGNATURE, usagesList.get(1)); - assertEquals(CertificateRequestSpec.Usages.SERVER_AUTH, usagesList.get(2)); - assertEquals(CertificateRequestSpec.Usages.S_MIME, usagesList.get(3)); + assertEquals(5, 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)); } @Test diff --git a/java-generator/it/src/it/enum-ser-deser/src/test/resources/cert-manager.crds.1.7.1.yaml b/java-generator/it/src/it/enum-ser-deser/src/test/resources/cert-manager.crds.1.7.1.yaml index 7b305c26af2..6b8ecc7bb7d 100644 --- a/java-generator/it/src/it/enum-ser-deser/src/test/resources/cert-manager.crds.1.7.1.yaml +++ b/java-generator/it/src/it/enum-ser-deser/src/test/resources/cert-manager.crds.1.7.1.yaml @@ -140,6 +140,7 @@ spec: description: 'KeyUsage specifies valid usage contexts for keys. See: https://tools.ietf.org/html/rfc5280#section-4.2.1.3 https://tools.ietf.org/html/rfc5280#section-4.2.1.12 Valid KeyUsage values are as follows: "signing", "digital signature", "content commitment", "key encipherment", "key agreement", "data encipherment", "cert sign", "crl sign", "encipher only", "decipher only", "any", "server auth", "client auth", "code signing", "email protection", "s/mime", "ipsec end system", "ipsec tunnel", "ipsec user", "timestamping", "ocsp signing", "microsoft sgc", "netscape sgc"' type: string enum: + - "" - signing - digital signature - content commitment diff --git a/java-generator/it/src/it/enum-ser-deser/src/test/resources/sample1.yaml b/java-generator/it/src/it/enum-ser-deser/src/test/resources/sample1.yaml index de947136b7f..d88088c97d3 100644 --- a/java-generator/it/src/it/enum-ser-deser/src/test/resources/sample1.yaml +++ b/java-generator/it/src/it/enum-ser-deser/src/test/resources/sample1.yaml @@ -19,9 +19,10 @@ kind: CertificateRequest metadata: name: my-ca-cr spec: - request: LS0tL + request: dGVzdAo= isCA: false usages: + - "" - signing - digital signature - server auth