Skip to content

Fix implicit null converted to empty string in flow mappings - #331

Open
reginaldalfret wants to merge 1 commit into
google:mainfrom
reginaldalfret:fix/reginald-yamlfmt-flow-mapping-implicit-null
Open

reginaldalfret wants to merge 1 commit into
google:mainfrom
reginaldalfret:fix/reginald-yamlfmt-flow-mapping-implicit-null

Conversation

@reginaldalfret

Copy link
Copy Markdown

Fixes #320

Summary

Fixes an issue where implicit null scalars within flow mappings (such as a: {b: } or GitHub Actions workflow dispatch blocks on: { push: { branches: [main] }, workflow_dispatch: }) were converted into empty single-quoted strings '' (e.g. a: {b: ''}).

Root Cause

In pkg/yaml/emitterc.go, an empty scalar node was marked with flow_plain_allowed = false and len == 0 && emitter.flow_level > 0 unconditionally forced style = yaml_SINGLE_QUOTED_SCALAR_STYLE. This caused implicit null values in flow mappings to fall back to single-quoted empty strings ''.

Changes

  1. Set emitter.scalar_data.flow_plain_allowed = true for empty scalar values in yaml_emitter_analyze_scalar.
  2. Changed the condition in yaml_emitter_select_scalar_style from len(value) == 0 && (emitter.flow_level > 0 || emitter.simple_key_context) to only check len(value) == 0 && emitter.simple_key_context.
  3. Fixed stripTrailingNewline in formatter_test.go to properly strip CRLF endings.
  4. Added test cases in pkg/yaml/node_test.go (TestFlowMappingImplicitNull) and formatters/basic/formatter_test.go.

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

Successfully merging this pull request may close these issues.

basic formatter converts an implicit null to an empty string in flow mappings

1 participant