Skip to content

Commit

Permalink
[generator] Fix <remove-attr/> metadata (#999)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jpobst authored Jun 30, 2022
1 parent 265ad76 commit 7f1d2d7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void Apply (ApiXmlDocument apiDocument, string apiLevelString, int produc
var matched = false;

foreach (var node in nodes) {
node.RemoveAttributes ();
node.Attributes (name).Remove ();
matched = true;
}

Expand Down
14 changes: 13 additions & 1 deletion tests/generator-Tests/Unit-Tests/FixupXmlDocumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,19 @@ public void RemoveAttribute ()

api.ApplyFixupFile (fixup);

Assert.AreEqual ("<api><package /><package name='java' jni-name='java' /></api>", api.ApiDocument.ToString (SaveOptions.DisableFormatting).Replace ('\"', '\''));
Assert.AreEqual ("<api><package jni-name='android' /><package name='java' jni-name='java' /></api>", api.ApiDocument.ToString (SaveOptions.DisableFormatting).Replace ('\"', '\''));
}

[Test]
public void RemoveNotFoundAttribute ()
{
// Attribute 'foo' doesn't exist on node
var api = GetXmlApiDocument ();
var fixup = GetFixupXmlDocument ("<remove-attr path=\"/api/package[@name='android']\" name='foo' />");

api.ApplyFixupFile (fixup);

Assert.AreEqual ("<api><package name='android' jni-name='android' /><package name='java' jni-name='java' /></api>", api.ApiDocument.ToString (SaveOptions.DisableFormatting).Replace ('\"', '\''));
}

[Test]
Expand Down

0 comments on commit 7f1d2d7

Please sign in to comment.