Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unmarshaling an invalid YAML is not throwing an error #112

Closed
kishorviswanathan opened this issue Jul 9, 2024 · 7 comments
Closed

Unmarshaling an invalid YAML is not throwing an error #112

kishorviswanathan opened this issue Jul 9, 2024 · 7 comments

Comments

@kishorviswanathan
Copy link

Summary

Unmarshaling an invalid YAML with yaml.Unmarshal populates struct with invalid data rather than returning an error.

Example

package main

import (
	"fmt"
	"sigs.k8s.io/yaml"
)

const (
	invalidYAML = `
  test:
test1: hello
`
)

type yamlType struct {
	Test  string `yaml:"test"`
	Test1 string `yaml:"test1"`
}

func main() {
	var data yamlType
	err := yaml.Unmarshal([]byte(invalidYAML), &data)
	if err != nil {
		fmt.Printf("Error %e", err)
	}
	fmt.Printf("data: %+v\n", data)
}

Output:

data: map[test:<nil>]

Expected output

The function should throw an error since the YAML is not valid.

Additional Information

@dims
Copy link
Member

dims commented Jul 9, 2024

try UnmarshalStrict

/close

@k8s-ci-robot
Copy link

@dims: Closing this issue.

In response to this:

try UnmarshalStrict

/close

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.

@kishorviswanathan
Copy link
Author

Makes no difference if this is what you meant.

package main

import (
	"fmt"

	"sigs.k8s.io/yaml"
)

const (
	invalidYAML = `
  test:
test1: hello
`
)

type yamlType struct {
	Test  string `yaml:"test"`
	Test1 string `yaml:"test1"`
}

func main() {
	var data1 interface{}
	err := yaml.UnmarshalStrict([]byte(invalidYAML), &data1)
	if err != nil {
		fmt.Printf("Error1 %e", err)
	}

	fmt.Printf("data1: %+v\n", data1)

	var data2 yamlType
	err = yaml.UnmarshalStrict([]byte(invalidYAML), &data2)
	if err != nil {
		fmt.Printf("Error2 %e", err)
	}
	fmt.Printf("data2: %+v\n", data2)
}

Output:

data1: map[test:<nil>]
data2: {Test: Test1:}

@kishorviswanathan
Copy link
Author

@dims I am not sure if this issue should remain closed as I was able to reproduce the bug with UnmarshalStrict as well. I don't have permission to reopen it.

@anderseknert
Copy link

I too would want to know more about this behavior. Thanks for looking into it @kishorviswanathan 👍

@gusega
Copy link

gusega commented Sep 26, 2024

Hi all, any update on this?

@Seraphyno
Copy link

Hi, I'm also following this thread, currently blocked by this upstream issue as I'm using OPA which relies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants