Skip to content

Commit

Permalink
[java-generator] Fix encoding for empty strings in enums
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaTP committed Jan 16, 2023
1 parent f7f9a46 commit e5e590b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
*/
package io.fabric8.java.generator.nodes;

import com.fasterxml.jackson.annotation.JsonValue;
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;
Expand Down Expand Up @@ -56,7 +59,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
Expand All @@ -77,6 +84,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 NameExpr("return value")));
getValue.addAnnotation("com.fasterxml.jackson.annotation.JsonValue");

for (String k : this.values) {
String constantName;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ void testDeserialization() {
List<CertificateRequestSpec.Usages> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ kind: CertificateRequest
metadata:
name: my-ca-cr
spec:
request: LS0tL
request: dGVzdAo=
isCA: false
usages:
- ""
- signing
- digital signature
- server auth
Expand Down

0 comments on commit e5e590b

Please sign in to comment.