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

encoding/xml: omit parent tags if value is empty #2 #7233

Open
kdar opened this issue Jan 29, 2014 · 17 comments

Comments

Projects
None yet
@kdar
Copy link

commented Jan 29, 2014

What steps will reproduce the problem?
http://play.golang.org/p/m2rU4cPOBo

What is the expected output?
<OmitEmpty></OmitEmpty>

What do you see instead?
<OmitEmpty><Vars></Vars></OmitEmpty>

Please provide any additional information below.
https://golang.org/cl/6569067/

This was originally: https://golang.org/issue/4168
Ran into this issue today making a restful API service. Ended up having to make my own
custom Marshaller.
@LeGEC

This comment has been minimized.

Copy link

commented Feb 12, 2014

Comment 1:

Further comments on this issue:
In this case, the "omitempty" directive has actually no effect :
http://play.golang.org/p/HG7io7mH7l
IMHO, the extra ",omitempty" should be taken as a clear directive in this case (1-step
deep node list).
For a deeper nested list, the "expected behavior" is ambiguous :
type Ambiguous struct {
    Bees []string `xml:"Beez>Bees>Bee,omitempty"` //I don't have a "natural" argument for this one ...
}
My choice would be : ommit the base "Beez" node in the output.
One point for this choice :
- if we ommit the base node, there still is a way to write a type structure which will
use only xml annotations and which will contain "<Beez></Beez>" in the
Marshal-ed output :
type Disambiguated struct {
    Beez Nested `xml:"Beez"`
}
type Nested struct {
    Bees []string `xml:"Bees>Bee,omitempty"`
}
- if we don't ommit the base node, I don't see how to achieve this without writing a
custom Marshaller.
Regarding the "break backward compatibility" part :
 * it shouldn't break the Unmarshaling of a previously Marshal-ed structure,
 * it could indeed change the output of a program, but as I stated, I think this output is the one expected by the programmer if ",omitempty" is specified.
This is an adapted repost of this comment :
https://golang.org/issue/4168?c=10
@rsc

This comment has been minimized.

Copy link
Contributor

commented Mar 3, 2014

Comment 2:

Are you asking for an omitparent tag? Because we're still not going to change the
behavior of omitempty at this point.
@rsc

This comment has been minimized.

Copy link
Contributor

commented Mar 4, 2014

Comment 3:

Status changed to WaitingForReply.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Mar 4, 2014

Comment 4:

Labels changed: added release-none.

@kdar

This comment has been minimized.

Copy link
Author

commented Mar 4, 2014

Comment 5:

Well, I guess that would be the only option given the Go1 compat guarantee.
@LeGEC

This comment has been minimized.

Copy link

commented Mar 4, 2014

Comment 6:

@rsc : I'd rather have the modified behavior in 1.3.1 or 1.4 than add an extra directive
just for this corner case.
@gopherbot

This comment has been minimized.

Copy link

commented Jul 23, 2014

Comment 7 by TheDonDaniello:

I have the same problem: no way to get rid of the parent tag even if it's marked with
omitempty and does not have any children or attributes.
@griesemer

This comment has been minimized.

Copy link
Contributor

commented Oct 1, 2014

Comment 8:

Labels changed: added repo-main.

@sshikaree

This comment has been minimized.

Copy link

commented Jan 11, 2015

You can use a pointer to a structure. While it's uninitialized, pointer is nil and omited by marshaller.
http://play.golang.org/p/tdiGkAdQAQ
But I think it would be more convenient if we could also omit the structure ("Time" in example).

@rsc rsc added this to the Unplanned milestone Apr 10, 2015

@rsc rsc removed release-none labels Apr 10, 2015

mvdan added a commit to mvdan/gexf that referenced this issue Sep 17, 2015

Don't work around omitempty issue with *[]
See golang/go#7233. Prefer correct Go over slightly
lighter XML.
@mvdan

This comment has been minimized.

Copy link
Member

commented Sep 17, 2015

@rsc improving xml namespaces support (#9519) is targeted at 1.6. It modifies the output of xml marshaling in a similar way to what is proposed here.

Could this issue be considered for 1.6 too?

On a previous issue you said this wasn't important enough to break compatibility for, yet the namespace issue is technically breaking compatibility as well. Although both changes should still make the marshaling output valid xml.

@ianlancetaylor ianlancetaylor modified the milestones: Go1.6, Unplanned Sep 17, 2015

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Sep 17, 2015

I changed the milestone to Go1.6, but somebody still has to actually do the work.

@mvdan

This comment has been minimized.

Copy link
Member

commented Sep 17, 2015

@kdar's patch in https://golang.org/cl/6569067/ can probably still be applied.

@rsc rsc changed the title encoding/xml: Omit parent tags if value is empty #2 encoding/xml: omit parent tags if value is empty #2 Nov 5, 2015

@rsc

This comment has been minimized.

Copy link
Contributor

commented Nov 25, 2015

Will look at with #13400 in Go 1.7.

@rsc rsc modified the milestones: Go1.7, Go1.6 Nov 25, 2015

@rsc rsc modified the milestones: Go1.8, Go1.7 May 18, 2016

@quentinmit quentinmit added the NeedsFix label Oct 7, 2016

@quentinmit quentinmit added the Blocked label Oct 7, 2016

@rsc rsc modified the milestones: Go1.9Early, Go1.8 Oct 26, 2016

@bradfitz bradfitz modified the milestones: Go1.10Early, Go1.9Early May 3, 2017

@bradfitz bradfitz modified the milestones: Go1.10Early, Go1.10 Jun 14, 2017

@rsc rsc modified the milestones: Go1.10, Go1.11 Nov 22, 2017

@aktungmak

This comment has been minimized.

Copy link

commented Jan 9, 2018

Is this likely to get merged anytime soon? It seems that all the work is done.

It is a real deal breaker when handling NETCONF XML since most vendors do not accept empty tags. Having this work properly will really unleash the declarative nature of struct tags.

@SamWhited

This comment has been minimized.

Copy link
Member

commented Jan 9, 2018

The Go 1.10 tree is already closed and this has been moved to Go 1.11 which will open sometime in February.

@aktungmak

This comment has been minimized.

Copy link

commented Jan 9, 2018

OK, here's hoping it gets included and this trivial issue can be put to bed.

@iWdGo

This comment has been minimized.

Copy link
Contributor

commented Mar 28, 2018

While reading all encoding/xml issues together, documentation of encoding/xml states exceptions to rules based on tags applying to field including "omitempty" and hyphen ("-") which is independent of value. The following paragraph of the documentation states that a field is nested in its parent tags. This proposal implies that the parents are omitted when the documented tag applies to the field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.