Skip to content

GetFieldValue returns string "null" for null field value #6317

Open

Description

@tal-sapan

What happened?

GetFieldValue returns a string value "null" when a yaml field is null. There is no way to differentiate between the 2 cases based on the return value, for example to check if a field of a different expected type is null or has an incorrect type.

What did you expect to happen?

I expected nil to be returned in that case.

How can we reproduce it (as minimally and precisely as possible)?

package main

import (
	"fmt"
	"strings"

	"sigs.k8s.io/kustomize/kyaml/kio"
	"sigs.k8s.io/kustomize/kyaml/yaml"
)

const input = `
kind: TestKind
metadata:
  name: hello-world
spec:
  fieldWithNullValue: null
  fieldWithStringNullValue: "null"
`

func main() {
	data, err := kio.ParseAll(input)
	if err != nil {
		fmt.Println("error: ", err)
		return
	}

	checkFieldValue(data[0], "spec", "fieldWithNullValue")
	checkFieldValue(data[0], "spec", "fieldWithStringNullValue")
}

func checkFieldValue(node *yaml.RNode, fieldPath ...string) {
	rNode, err := node.Pipe(yaml.Lookup(fieldPath...))
	if err != nil {
		fmt.Println("error: ", err)
		return
	}
	fullPath := strings.Join(fieldPath, ".")
	if rNode == nil {
		fmt.Println("field not found: ", fullPath)
		return
	}
	fmt.Println(fmt.Sprintf("%s tag: %s, value: %q", fullPath, rNode.YNode().Tag, rNode.YNode().Value))

	f, err := node.GetFieldValue(fullPath)
	if err != nil {
		fmt.Println("error: ", err)
		return
	}
	fmt.Println(fmt.Sprintf("%s type: %T, value: %q", fullPath, f, f))
}

Expected output

spec.fieldWithNullValue tag: !!null, value: "null"
spec.fieldWithNullValue type: <nil>, value: %!q(<nil>)
spec.fieldWithStringNullValue tag: !!str, value: "null"
spec.fieldWithStringNullValue type: string, value: "null"

Actual output

spec.fieldWithNullValue tag: !!null, value: "null"
spec.fieldWithNullValue type: string, value: "null"
spec.fieldWithStringNullValue tag: !!str, value: "null"
spec.fieldWithStringNullValue type: string, value: "null"

Kustomize version

sigs.k8s.io/kustomize/kyaml v0.21.1

Operating system

Windows

Activity

  1. added
    kind/bugCategorizes issue or PR as related to a bug.
    on Oct 8, 2026
  2. kubernetes-prow commented on Oct 8, 2026

    @kubernetes-prow
    Contributor

    This issue is currently awaiting triage.

    SIG CLI takes a lead on issue triage for this repo, but any Kubernetes member can accept issues by applying the triage/accepted label.

    The triage/accepted label can be added by org members by writing /triage accepted in a comment.

    Details

    Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

  3. added
    needs-triageIndicates an issue or PR lacks a `triage/foo` label and requires one.
    on Oct 8, 2026
  4. tal-sapan commented on Oct 8, 2026

    @tal-sapan
    Author

    The solution is probably to add handlng for the null case here:

    switch yn.Tag {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugCategorizes issue or PR as related to a bug.needs-triageIndicates an issue or PR lacks a `triage/foo` label and requires one.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions