[MDSAL-2] Naming conflict appears when key of list is constructed from leaf with name key Created: 20/Nov/13  Updated: 04/Jun/18  Resolved: 04/Jun/18

Status: Resolved
Project: mdsal
Component/s: Binding codegen
Affects Version/s: None
Fix Version/s: Fluorine

Type: Improvement
Reporter: Martin Vitez Assignee: Robert Varga
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Operating System: All
Platform: PC


Issue Links:
Duplicate
is duplicated by MDSAL-159 Unable to generate sources with CodeG... Resolved
is duplicated by YANGTOOLS-357 Binding Java API Generator -> ClassCa... Resolved
Relates
relates to MDSAL-244 @SuppressWarnings("all") on all gener... Resolved
relates to TSC-101 MDSAL Fluorine API breakage window 3 Resolved

 Description   

With following statement in yang model:

list property {
key "key";
leaf key

{ type string; }

}

exception is thrown:

java.lang.ClassCastException: org.opendaylight.yangtools.binding.generator.util.Types$ConcreteTypeImpl cannot be cast to org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject
at org.opendaylight.yangtools.sal.java.api.generator.BuilderTemplate.generateConstructor(BuilderTemplate.java:1006)
at org.opendaylight.yangtools.sal.java.api.generator.BuilderTemplate.body(BuilderTemplate.java:398)
at org.opendaylight.yangtools.sal.java.api.generator.BaseTemplate.generate(BaseTemplate.java:73)
at org.opendaylight.yangtools.sal.java.api.generator.BuilderGenerator.generate(BuilderGenerator.java:58)
at org.opendaylight.yangtools.sal.java.api.generator.GeneratorJavaFile.generateTypeToJavaFile(GeneratorJavaFile.java:126)
at org.opendaylight.yangtools.sal.java.api.generator.GeneratorJavaFile.generateToFile(GeneratorJavaFile.java:73)

This is caused because of 2 methods with same name (getKey) are generated, first for list statement to get key, second as a getter for leaf node.



 Comments   
Comment by Tony Tkacik [ 27/Feb/14 ]

Solution is to rename getter getKey(), which was added by BindingGenerator
to key(). Methods starting with get... should be left to be only defined by YANG.

This is API breakage, so it needs to be part of binding specification v2.

Comment by Jozef Gloncak [ 03/Mar/14 ]

Working on it

Comment by Jozef Gloncak [ 03/Mar/14 ]

status changed

Comment by Jozef Gloncak [ 04/Mar/14 ]

According to RFC 6020 name of concrete container, leaf, leaf-list, list,
grouping, choice ... has to start with letter which is followed with
letters, digits, '_', '-', or '.' character.

Before this commit there was generated for key of a list getter method
'getKey'. 'key' word was used while getter method was constructed.
In this commit 'key$' word is used. Dollar is character which can't be used in
YANG but can be used in JAVA. Therefore it can't happen that list
subelement identifier will be also 'key$'.

As was specified in bugzilla - interface (from YANG list) and implementing
class are generated with 'key()' getter method and with member field '_key$'.
Builder for implementing class is generated with member field
'_key$' and with 'keyGet()' and 'keySet()' getter and setter methods.

Name conflict for methods 'key()', 'keySet()', 'keyGet()' is solved
because all methods generated from subelements of YANG list starts with
'get', 'set' or 'is'.

Currently all yangtools projects were succesfuly built with this change. The commit is in draft state
https://git.opendaylight.org/gerrit/#/c/5521/

Comment by Jozef Gloncak [ 05/Mar/14 ]

test if methods 'get()', 'keyGet()', 'keySet()' are in builder, interface and interface implementation was added

Comment by Jozef Gloncak [ 30/Apr/14 ]

The change was rebased on 30APR2014

Comment by Sven Wisotzky [ 31/Oct/16 ]

There are much more flavors of this error:
The problem basically always occurs, when a list has a child object named "key". So I've found a case, where a list contains a container called key which causes basically the same issue.

In general I would agree, that renaming the method "getKey()" to something else like "whatKey()" would fix that issue. I am only concerned about the ongoing discussion, that getKey() might still be available for compatibility with existing "yang binding v1" integration projects - and it gets only removed in cases where naming conflicts occur. If going down that path, implementation should be smart enough, to not only check for leaves called "key" but other types like container or list as well.

I am surprised that this is categorized as with importance "lowest" and "enhancement". The thing is basically, that this break with existing vendor YANG models - as key is very likely used as YANG variable name around protocol authentication and privacy (for example BGP keychains, IPsec, ...). In my eyes this is not just a corner case - it is a bug with major severity, beside we come to such conclusion that ODL should not be flexible enough to compile any valid/existing YANG model in the market.

Comment by Jakub Toth [ 24/Mar/17 ]

https://git.opendaylight.org/gerrit/#/c/53279/10

Comment by Robert Varga [ 23/Apr/18 ]

Fluorine Binding V1 is cleared for this API breakage. Builders will use 'withKey()' instead of 'setKey()'. This also affects augmentations, where we need to just solve 'getAugmentation()', renaming it to 'augmentation()'.

Generated at Wed Feb 07 20:08:23 UTC 2024 using Jira 8.20.10#820010-sha1:ace47f9899e9ee25d7157d59aa17ab06aee30d3d.