Skip to content

Commit 7f1d2d7

Browse files
authored
[generator] Fix <remove-attr/> metadata (#999)
Fixes: #976 Given an `api.xml` element like: <package name='my.package'> <class name='MyClass' api-since='29' /> </package> One would expect to be able to remove the `class/@api-since` attribute with this metadata: <remove-attr path='/api/package[@name='my.package']/class[@name='MyClass']' name='api-since' /> However, even though `generator` reads the `name` attribute, it then proceeds to ignore it and remove *all* attributes on the matched node, resulting in: <package name='my.package'> <class /> </package> This seems unintuitive, and I'm not sure how this could ever be successfully used in a binding project, since leaving an empty `<class/>`/etc. node causes `generator` to crash. Update `<remove-attr/>` to work as expected, removing the named attribute.
1 parent 265ad76 commit 7f1d2d7

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/Java.Interop.Tools.Generator/Metadata/FixupXmlDocument.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public void Apply (ApiXmlDocument apiDocument, string apiLevelString, int produc
153153
var matched = false;
154154

155155
foreach (var node in nodes) {
156-
node.RemoveAttributes ();
156+
node.Attributes (name).Remove ();
157157
matched = true;
158158
}
159159

tests/generator-Tests/Unit-Tests/FixupXmlDocumentTests.cs

+13-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,19 @@ public void RemoveAttribute ()
8282

8383
api.ApplyFixupFile (fixup);
8484

85-
Assert.AreEqual ("<api><package /><package name='java' jni-name='java' /></api>", api.ApiDocument.ToString (SaveOptions.DisableFormatting).Replace ('\"', '\''));
85+
Assert.AreEqual ("<api><package jni-name='android' /><package name='java' jni-name='java' /></api>", api.ApiDocument.ToString (SaveOptions.DisableFormatting).Replace ('\"', '\''));
86+
}
87+
88+
[Test]
89+
public void RemoveNotFoundAttribute ()
90+
{
91+
// Attribute 'foo' doesn't exist on node
92+
var api = GetXmlApiDocument ();
93+
var fixup = GetFixupXmlDocument ("<remove-attr path=\"/api/package[@name='android']\" name='foo' />");
94+
95+
api.ApplyFixupFile (fixup);
96+
97+
Assert.AreEqual ("<api><package name='android' jni-name='android' /><package name='java' jni-name='java' /></api>", api.ApiDocument.ToString (SaveOptions.DisableFormatting).Replace ('\"', '\''));
8698
}
8799

88100
[Test]

0 commit comments

Comments
 (0)