java icon indicating copy to clipboard operation
java copied to clipboard

java API annotated with gson annotations while jackson serializer is used

Open tipame opened this issue 6 months ago • 2 comments

Describe the bug All Kubernates Java API objects annonated with Gson annotation like @SerializedName and @JsonAdapter At the same time io.kubernetes.client.util.Yaml uses SnakeYaml which in turn uses Jackson and do not respect gson annotations. It somehow works only because most of the names in java representation are the same as names in json. But not always! I generated a java api for networking.istio.io/v1 (using instruction https://github.com/kubernetes-client/java/blob/master/docs/generate-model-from-third-party-resources.md) And now i have isue with deserializing V1ServiceEntrySpec as it contains enum with differend names for java and json reperesentations:

@JsonAdapter(LocationEnum.Adapter.class)
  public enum LocationEnum {
    EXTERNAL("MESH_EXTERNAL"),

    INTERNAL("MESH_INTERNAL");
}
Unable to find enum value 'MESH_EXTERNAL' for enum class: io.istio.models.V1ServiceEntrySpec$LocationEnum
 in 'reader', line 15, column 13:
      location: MESH_EXTERNAL # Indicates this s ... 
                ^

	at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:283)
	at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:169)
	at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:265)
	at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:248)
	at io.kubernetes.client.util.Yaml$CustomConstructor.constructObject(Yaml.java:247)
	at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.newInstance(Constructor.java:295)
	at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:260)
	... 14 more
Caused by: org.yaml.snakeyaml.error.YAMLException: Unable to find enum value 'MESH_EXTERNAL' for enum class: io.istio.models.V1ServiceEntrySpec$LocationEnum
	at org.yaml.snakeyaml.constructor.Constructor$ConstructScalar.constructStandardJavaInstance(Constructor.java:481)
	at org.yaml.snakeyaml.constructor.Constructor$ConstructScalar.construct(Constructor.java:365)
	at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:265)
	at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:248)
	at io.kubernetes.client.util.Yaml$CustomConstructor.constructObject(Yaml.java:247)
	at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:261)
	... 20 more

Client Version e.g. 23.0.0

Java Version e.g. Java 17

To Reproduce Change java representation name for any property annotated with @SerializedName and @JsonAdapter in any KubernetesObject and try to deserialize object from yaml: V1ServiceEntry se = Yaml.loadAs(new File("dummy-service-entry.yaml"), V1ServiceEntry.class);

Expected behavior All Kubernetes API objects should be annotated with jackson annotations.

tipame avatar Sep 01 '25 09:09 tipame

The library uses GSON when serializing to JSON to send over the wire to the Kubernetes API server, so we need to retain the GSON annotations.

We'd be happy to take PRs to add the Jackson annotations also if that would work. I don't know if it is feasible to switch SnakeYAML to GSON (I'm guessing "no") but that's another option.

brendandburns avatar Sep 01 '25 15:09 brendandburns

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle stale
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

k8s-triage-robot avatar Nov 30 '25 15:11 k8s-triage-robot