`attributesGroupName` writes `null` and `undefined` attribute values as text

Open Beginner friendly
#35 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
83/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript
Domain
backend

Research direction

Start in src/fxb.js, comparing the individually keyed attribute handling around lines 170–183 with the attributesGroupName branch around lines 281–288. Check the existing tests for grouped attributes and add coverage for null and undefined values. Done means grouped and individually keyed attributes both omit those values and the remaining attribute output matches the expected XML.

Written by the indexing model from the issue text.

Description

Summary

Version: 1.3.2
Context: I group attributes so that attribute keys don't need the @_ prefix and can't collide with child element names.

With attributesGroupName set, an attribute whose value is null or undefined is written as the text "null" or "undefined". Without a group name, the same values are left out.

Reproduction

import XMLBuilder from 'fast-xml-builder';

const perKey = new XMLBuilder({ ignoreAttributes: false });
console.log(perKey.build({ item: { '@_a': 'x', '@_b': null, '@_c': undefined } }));
// <item a="x"></item>

const grouped = new XMLBuilder({ ignoreAttributes: false, attributesGroupName: '$' });
console.log(grouped.build({ item: { $: { a: 'x', b: null, c: undefined } } }));
// <item a="x" b="null" c="undefined"></item>

Expected

Both print <item a="x"></item>.

Additional details

When building, the builder skips undefined and null values for individually keyed attributes (src/fxb.js#L170-L183), which came from NaturalIntelligence/fast-xml-parser#598 for NaturalIntelligence/fast-xml-parser#585. The attributesGroupName branch (src/fxb.js#L281-L288) passes every value in the group to buildAttrPairStr without that check. attributeValueProcessor can't drop an attribute, so no option works around it.

Dominant language
JavaScript
Stars
22
Forks
9
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from NaturalIntelligence/fast-xml-builder

All issues in NaturalIntelligence/fast-xml-builder

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.