From c55d9a06ce5405fd01acc3cf8bf6d19220d0466e Mon Sep 17 00:00:00 2001 From: Fabio Bonelli Date: Mon, 30 Nov 2026 15:47:37 +0000 Subject: [PATCH] fix: preserve text resembling NaN or infinity in YAML export --- marshallers.go | 30 ++++++++++++++++ v0.go | 3 +- v1.go | 3 +- yaml_export_test.go | 83 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 yaml_export_test.go diff --git a/marshallers.go b/marshallers.go index 1dd6302..108b716 100644 --- a/marshallers.go +++ b/marshallers.go @@ -3,10 +3,14 @@ package publiccode import ( "encoding/json" "errors" + "fmt" "net/url" "reflect" + "strings" yaml "github.com/goccy/go-yaml" + "github.com/goccy/go-yaml/ast" + "github.com/goccy/go-yaml/token" ) type URL url.URL //nolint:recvcheck // String must use value receiver to implement fmt.Stringer on URL @@ -91,3 +95,29 @@ func (a *UrlOrUrlArray) UnmarshalYAML(unmarshal func(any) error) error { return nil } + +// marshalYAML quotes strings that would change when decoded as plain scalars. +func marshalYAML(v any) ([]byte, error) { + node, err := yaml.ValueToNode(v) + if err != nil { + return nil, fmt.Errorf("encoding YAML node: %w", err) + } + + ast.Walk(stringQuoter{}, node) + + return []byte(node.String() + "\n"), nil +} + +type stringQuoter struct{} + +//nolint:ireturn // ast.Visitor requires this return type. +func (q stringQuoter) Visit(node ast.Node) ast.Visitor { + if s, ok := node.(*ast.StringNode); ok { + switch strings.ToLower(s.Value) { + case ".nan", ".inf", "+.inf", "-.inf": + s.Token.Type = token.DoubleQuoteType + } + } + + return q +} diff --git a/v0.go b/v0.go index 7bdea25..51c48c8 100644 --- a/v0.go +++ b/v0.go @@ -3,7 +3,6 @@ package publiccode import ( "fmt" - yaml "github.com/goccy/go-yaml" urlutil "github.com/publiccodeyml/libpubliccode/v5/internal" ) @@ -174,7 +173,7 @@ func (p PublicCodeV0) ToYAML() ([]byte, error) { p.It = nil } - b, err := yaml.Marshal(p) + b, err := marshalYAML(p) if err != nil { return nil, fmt.Errorf("marshaling to YAML: %w", err) } diff --git a/v1.go b/v1.go index d9714ae..323d43e 100644 --- a/v1.go +++ b/v1.go @@ -3,7 +3,6 @@ package publiccode import ( "fmt" - yaml "github.com/goccy/go-yaml" urlutil "github.com/publiccodeyml/libpubliccode/v5/internal" ) @@ -117,7 +116,7 @@ func (p PublicCodeV1) Version() uint { } func (p PublicCodeV1) ToYAML() ([]byte, error) { - b, err := yaml.Marshal(p) + b, err := marshalYAML(p) if err != nil { return nil, fmt.Errorf("marshaling to YAML: %w", err) } diff --git a/yaml_export_test.go b/yaml_export_test.go new file mode 100644 index 0000000..b2f49ce --- /dev/null +++ b/yaml_export_test.go @@ -0,0 +1,83 @@ +package publiccode + +import ( + "bytes" + "encoding/json" + "os" + "reflect" + "strings" + "testing" + + "github.com/goccy/go-yaml" +) + +func TestToYAMLPreservesSpecialFloatText(t *testing.T) { + input, err := os.ReadFile("testdata/v0/valid/valid.minimal.yml") + if err != nil { + t.Fatal(err) + } + parser, err := NewParser(ParserConfig{DisableExternalChecks: true}) + if err != nil { + t.Fatal(err) + } + + for _, value := range []string{ + ".nan", ".NaN", ".NAN", ".inf", ".Inf", ".INF", + "-.inf", "-.Inf", "-.INF", "+.inf", "+.Inf", "+.INF", + "NaN", "+Inf", "-Inf", "true", "null", "1.0", + } { + t.Run(value, func(t *testing.T) { + quoted, err := json.Marshal(value) + if err != nil { + t.Fatal(err) + } + source := strings.ReplaceAll(string(input), "Medusa", string(quoted)) + source = strings.ReplaceAll(source, "Just one feature", string(quoted)) + before, err := parser.ParseStream(strings.NewReader(source)) + if err != nil { + t.Fatal(err) + } + parsed, ok := before.(PublicCodeV0) + if !ok { + t.Fatalf("parsed type = %T, want PublicCodeV0", before) + } + if parsed.Name != value { + t.Fatalf("parsed name = %q, want %q", parsed.Name, value) + } + exported, err := before.ToYAML() + if err != nil { + t.Fatal(err) + } + after, err := parser.ParseStream(bytes.NewReader(exported)) + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(before, after) { + t.Errorf("document changed after YAML export:\nbefore: %#v\nafter: %#v", before, after) + } + + // Version 1 is not supported by ParseStream yet, but has its own exporter. + v1 := PublicCodeV1{ + Name: value, + Description: map[string]DescV1{ + "en": {LocalisedName: &value, Features: &[]string{value}, LongDescription: "First line\nsecond line\n"}, + }, + } + exported, err = v1.ToYAML() + if err != nil { + t.Fatal(err) + } + var reread PublicCodeV1 + if err := yaml.Unmarshal(exported, &reread); err != nil { + t.Fatal(err) + } + if reread.Name != value || reread.Description["en"].LocalisedName == nil || + *reread.Description["en"].LocalisedName != value || + reread.Description["en"].Features == nil || + !reflect.DeepEqual(*reread.Description["en"].Features, []string{value}) || + reread.Description["en"].LongDescription != v1.Description["en"].LongDescription { + t.Errorf("version 1 document changed after YAML export: %s", exported) + } + }) + } +}