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

go-yaml v1.14.3 panic on the the input JSON string for the map slice type. #543

Closed
Peefy opened this issue Nov 18, 2024 · 5 comments · Fixed by #546
Closed

go-yaml v1.14.3 panic on the the input JSON string for the map slice type. #543

Peefy opened this issue Nov 18, 2024 · 5 comments · Fixed by #546
Labels
bug Something isn't working

Comments

@Peefy
Copy link

Peefy commented Nov 18, 2024

Describe the bug
A clear and concise description of what the bug is.

To Reproduce

Please provide a minimum yaml content that can be reproduced.
We are more than happy to use Go Playground

Use the go 1.23 and github.com/goccy/go-yaml v1.14.3 to run the following test, it will panic with the error message but github.com/goccy/go-yaml v1.13.8 work fine

func TestPanicJsonYamlMapSlice(t *testing.T) {
	code := `
{
	"apiVersion": "apps/v1",
	"kind": "Deployment",
	"metadata": {
		"name": "nginx-deployment",
		"labels": {
			"app": "nginx"
		}
	},
	"spec": {
		"replicas": 3,
		"selector": {
			"matchLabels": {
				"app": "nginx"
			}
		},
		"template": {
			"metadata": {
				"labels": {
					"app": "nginx"
				}
			},
			"spec": {
				"containers": [
					{
						"name": "nginx-container",
						"image": "nginx:latest",
						"ports": [
							{
								"containerPort": 80
							}
						]
					}
				]
			}
		}
	}
}	
`
	yamlData := &yaml.MapSlice{}
	if err := yaml.UnmarshalWithOptions([]byte(code), yamlData, yaml.UseOrderedMap(), yaml.UseJSONUnmarshaler()); err != nil {
		panic(err)
	}
}

The error message is

panic: [2:1] found character '  ' that cannot start any token
           1 | {
        >  2 |  "apiVersion": "apps/v1",
               ^
           3 |  "kind": "Deployment",
           4 |  "metadata": {
           5 |          "name": "nginx-deployment", [recovered]
        panic: [2:1] found character '  ' that cannot start any token
           1 | {
        >  2 |  "apiVersion": "apps/v1",
               ^
           3 |  "kind": "Deployment",
           4 |  "metadata": {
           5 |          "name": "nginx-deployment",

Expected behavior
A clear and concise description of what you expected to happen.

Test passed

Screenshots
If applicable, add screenshots to help explain your problem.

Version Variables

  • Go version: [e.g. 1.21 ] 1.23
  • go-yaml's Version: [e.g. v1.11.1 ] v1.14.3

Additional context
Add any other context about the problem here.

@Peefy Peefy added the bug Something isn't working label Nov 18, 2024
@goccy
Copy link
Owner

goccy commented Nov 18, 2024

@Peefy Probably because of the use of the tab character. This appears to be the correct behavior, since YAML does not allow tabs to be specified for indent. For example, can go-yaml/yaml parse it ?

@Peefy
Copy link
Author

Peefy commented Nov 18, 2024

Sorry, I'm not sure how the YAML specification is defined, I have used the yaml.UseJSONUnmarshaler() flag to parse JSON strings with tab symbols. Should YAML Parser be compatible with this?

@Peefy
Copy link
Author

Peefy commented Nov 18, 2024

gopkg.in/yaml.v3 v3.0.1 is ok for this test case

package test

import (
	"testing"

	"gopkg.in/yaml.v3"
)

func TestGoYamlV3WithTab(t *testing.T) {
	code := `
{
	"apiVersion": "apps/v1",
	"kind": "Deployment",
	"metadata": {
		"name": "nginx-deployment",
		"labels": {
			"app": "nginx"
		}
	},
	"spec": {
		"replicas": 3,
		"selector": {
			"matchLabels": {
				"app": "nginx"
			}
		},
		"template": {
			"metadata": {
				"labels": {
					"app": "nginx"
				}
			},
			"spec": {
				"containers": [
					{
						"name": "nginx-container",
						"image": "nginx:latest",
						"ports": [
							{
								"containerPort": 80
							}
						]
					}
				]
			}
		}
	}
}	
`
	yamlData := make(map[string]any)
	if err := yaml.Unmarshal([]byte(code), yamlData); err != nil {
		panic(err)
	}
}

@goccy
Copy link
Owner

goccy commented Nov 18, 2024

Thank you for your additional information.
I see. It’s likely that in flow mode, allow tab characters for indentation—except in literal/folded styles—might be the correct behavior.

@goccy goccy mentioned this issue Nov 26, 2024
@goccy
Copy link
Owner

goccy commented Nov 26, 2024

@Peefy I've fixed this problem with v1.15.0 ! Please confirm it 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants