Skip to content

Commit

Permalink
feat(openapi): protobuf tag name value considered for json serialization
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa authored Oct 26, 2024
1 parent 0806a4e commit aa66d71
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
20 changes: 10 additions & 10 deletions kubernetes-model-generator/openapi/schemas/openshift-generated.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -66,15 +66,15 @@
public class DockerImageReference implements Editable<DockerImageReferenceBuilder> , 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<String, Object> additionalProperties = new LinkedHashMap<String, Object>();
Expand All @@ -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;
}
Expand Down

0 comments on commit aa66d71

Please sign in to comment.