XamlX icon indicating copy to clipboard operation
XamlX copied to clipboard

PR to fix issue #14370 in Avalonia project (2 of 2)

Open busitech opened this issue 7 months ago • 1 comments

The AvaloniaNameGenerator uses the TypeReferenceResolver when parsing markup files.

During parsing, when an element is found having an x:TypeArguments attribute and a property element, when the property element is visited the TypeReferenceResolver throws XamlTransformException: Unable to resolve type from namespace.

At the time of the exception, the declaring type for the property is what the TypeReferenceResolver is trying to resolve.

A property element is not allowed to have an x:TypeArguments attribute, so the TypeReferenceResolver naively tries to resolve the declaring class of the property with no type arguments, even when the declaring type on the property element and the type on the node above which has the x:TypeArguments are exactly the same name. This leads to the type not being found and the exception thrown.

How was the solution implemented (if it's not obvious)?

If the properties inside a tag are owned by exactly the same class (by name), use them for the child node.

Fixed issues

https://github.com/AvaloniaUI/Avalonia/issues/14370

busitech avatar Feb 02 '24 05:02 busitech

Thank you! Looks good to me in general. So, error was on deeper level than just Name Generator.

I think alternative solution would be to check if element property type is the same as parent node type. If so, then just reuse type instance instead of creating new XamlAstXmlTypeReference. Something like:

var declaringType = pair[0] == type.Name && elementNode.Name.NamespaceName == type.XmlNameSpace
    ? type : new XamlAstXmlTypeReference(el.AsLi(), elementNode.Name.NamespaceName, pair[0], parentGenericArguments);
var propertyReference = new XamlAstNamePropertyReference(el.AsLi(), declaringType, pair[1], type));
// And use propertyReference in the XamlAstXamlPropertyValueNode

Just in case if declaring type is different and might not have generic types. Could be the case with attached properties, I guess?

Either way, some tests would really help. Something like here https://github.com/kekekeks/XamlX/blob/master/tests/XamlParserTests/BasicCompilerTests.cs.

maxkatz6 avatar Feb 02 '24 06:02 maxkatz6

This fix has been optimized down to one line of code, a unit test has been added, and all tests pass.

busitech avatar Apr 23 '24 06:04 busitech

@busitech thank you! Looks good. Pinging @kekekeks to get it merged.

maxkatz6 avatar Apr 23 '24 08:04 maxkatz6