From aa66d71583a773e6b92bace2eb55e2201c8cb852 Mon Sep 17 00:00:00 2001 From: Marc Nuri Date: Sat, 26 Oct 2024 13:08:28 +0800 Subject: [PATCH] feat(openapi): protobuf tag name value considered for json serialization Signed-off-by: Marc Nuri --- .../pkg/openapi/openapi-gen-processors.go | 38 +++++++++++------- .../openapi/schemas/openshift-generated.json | 20 +++++----- .../api/model/DockerImageReference.java | 40 +++++++++---------- 3 files changed, 53 insertions(+), 45 deletions(-) diff --git a/kubernetes-model-generator/openapi/generator/pkg/openapi/openapi-gen-processors.go b/kubernetes-model-generator/openapi/generator/pkg/openapi/openapi-gen-processors.go index f13a488f77..406d362ef9 100644 --- a/kubernetes-model-generator/openapi/generator/pkg/openapi/openapi-gen-processors.go +++ b/kubernetes-model-generator/openapi/generator/pkg/openapi/openapi-gen-processors.go @@ -142,22 +142,30 @@ func processProtobufPackageOneOf(_ *generator.Context, pkg *types.Package) { func processProtobufTags(_ *generator.Context, _ *types.Package, t *types.Type, m *types.Member, memberIndex int) { tags := reflect.StructTag(m.Tags) protobufTag := tags.Get("protobuf") + if protobufTag == "" { + return + } jsonTag := tags.Get("json") - if protobufTag != "" && strings.Contains(protobufTag, "json=") { - // TODO, consider also name= (sometimes this is included instad of json=) - name := strings.Split(protobufTag, "json=")[1] - name = strings.Split(name, ",")[0] - var updatedJsonTag string - if strings.Contains(jsonTag, ",") { - updatedJsonTag = name + "," + strings.Split(jsonTag, ",")[1] - } else { - updatedJsonTag = name - } - if jsonTag == "" { - t.Members[memberIndex].Tags = t.Members[memberIndex].Tags + " json:\"" + updatedJsonTag+"\"" - } else { - t.Members[memberIndex].Tags = strings.Replace(t.Members[memberIndex].Tags, jsonTag, updatedJsonTag, 1) - } + var name string + if strings.Contains(protobufTag, "json=") { + name = strings.Split(strings.Split(protobufTag, "json=")[1], ",")[0] + + } else if strings.Contains(protobufTag, "name=") && jsonTag == "" { + name = strings.Split(strings.Split(protobufTag, "name=")[1], ",")[0] + } + if name == "" { + return + } + var updatedJsonTag string + if strings.Contains(jsonTag, ",") { + updatedJsonTag = name + "," + strings.Split(jsonTag, ",")[1] + } else { + updatedJsonTag = name + } + if jsonTag == "" { + t.Members[memberIndex].Tags = t.Members[memberIndex].Tags + " json:\"" + updatedJsonTag+"\"" + } else { + t.Members[memberIndex].Tags = strings.Replace(t.Members[memberIndex].Tags, jsonTag, updatedJsonTag, 1) } } diff --git a/kubernetes-model-generator/openapi/schemas/openshift-generated.json b/kubernetes-model-generator/openapi/schemas/openshift-generated.json index b6378c98f0..1f0f67b5c6 100644 --- a/kubernetes-model-generator/openapi/schemas/openshift-generated.json +++ b/kubernetes-model-generator/openapi/schemas/openshift-generated.json @@ -89238,34 +89238,34 @@ "description": "DockerImageReference points to a container image.", "type": "object", "required": [ - "Registry", - "Namespace", - "Name", - "Tag", - "ID" + "registry", + "namespace", + "name", + "tag", + "iD" ], "properties": { - "ID": { + "iD": { "description": "ID is the identifier for the container image", "type": "string", "default": "" }, - "Name": { + "name": { "description": "Name is the name of the container image", "type": "string", "default": "" }, - "Namespace": { + "namespace": { "description": "Namespace is the namespace that contains the container image", "type": "string", "default": "" }, - "Registry": { + "registry": { "description": "Registry is the registry that contains the container image", "type": "string", "default": "" }, - "Tag": { + "tag": { "description": "Tag is which tag of the container image is being referenced", "type": "string", "default": "" diff --git a/kubernetes-model-generator/openshift-model/src/generated/java/io/fabric8/openshift/api/model/DockerImageReference.java b/kubernetes-model-generator/openshift-model/src/generated/java/io/fabric8/openshift/api/model/DockerImageReference.java index 1f6fbc2715..099471e8f5 100644 --- a/kubernetes-model-generator/openshift-model/src/generated/java/io/fabric8/openshift/api/model/DockerImageReference.java +++ b/kubernetes-model-generator/openshift-model/src/generated/java/io/fabric8/openshift/api/model/DockerImageReference.java @@ -35,11 +35,11 @@ @JsonDeserialize(using = com.fasterxml.jackson.databind.JsonDeserializer.None.class) @JsonInclude(JsonInclude.Include.NON_NULL) @JsonPropertyOrder({ - "ID", - "Name", - "Namespace", - "Registry", - "Tag" + "iD", + "name", + "namespace", + "registry", + "tag" }) @ToString @EqualsAndHashCode @@ -66,15 +66,15 @@ public class DockerImageReference implements Editable , KubernetesResource { - @JsonProperty("ID") + @JsonProperty("iD") private String iD; - @JsonProperty("Name") + @JsonProperty("name") private String name; - @JsonProperty("Namespace") + @JsonProperty("namespace") private String namespace; - @JsonProperty("Registry") + @JsonProperty("registry") private String registry; - @JsonProperty("Tag") + @JsonProperty("tag") private String tag; @JsonIgnore private Map additionalProperties = new LinkedHashMap(); @@ -95,52 +95,52 @@ public DockerImageReference(String iD, String name, String namespace, String reg this.tag = tag; } - @JsonProperty("ID") + @JsonProperty("iD") public String getID() { return iD; } - @JsonProperty("ID") + @JsonProperty("iD") public void setID(String iD) { this.iD = iD; } - @JsonProperty("Name") + @JsonProperty("name") public String getName() { return name; } - @JsonProperty("Name") + @JsonProperty("name") public void setName(String name) { this.name = name; } - @JsonProperty("Namespace") + @JsonProperty("namespace") public String getNamespace() { return namespace; } - @JsonProperty("Namespace") + @JsonProperty("namespace") public void setNamespace(String namespace) { this.namespace = namespace; } - @JsonProperty("Registry") + @JsonProperty("registry") public String getRegistry() { return registry; } - @JsonProperty("Registry") + @JsonProperty("registry") public void setRegistry(String registry) { this.registry = registry; } - @JsonProperty("Tag") + @JsonProperty("tag") public String getTag() { return tag; } - @JsonProperty("Tag") + @JsonProperty("tag") public void setTag(String tag) { this.tag = tag; }