robshakir / pyangbind

A plugin for pyang that creates Python bindings for a YANG model.
Other
198 stars 120 forks source link

Primary key of a list cannot be of type string with a specific pattern #345

Closed CC-vga closed 2 months ago

CC-vga commented 4 months ago

I got an error when I decode an XML with valid value for an XML element. The XML element is declared as string with a given pattern in the yang model. And the 'valid' value matches the pattern. Here is an example of the YANG model, XML file and obtained error :)

Excerpt of the YANG model:

   typedef id-type {
    type string {
      pattern 'xyz';
    }
  }

  grouping node-grouping {
    leaf id {
      type id-type;
    }
    ...
  }

  container c-net {
    list nodes {
      key "id";
      uses node-grouping;
    }
    ...
  }

The whole YANG model is validated by pyang!

XML sample:

<MY_MODEL xmlns="MY_NAMESPACE">
    <c-net>
    <nodes>
        <id>xyz</id>
        ...
    </nodes>
    </c-net>
</MY_MODEL>

Error message:

'key value must be valid, {\'error-string\': \'id must be of a type compatible with id-type\', \'defined-type\': \'id-type\', \'generated-type\': \'YANGDynClass(base=RestrictedClassType(base_type=six.text_type, restriction_dict={\\\'pattern\\\': \\\'xyz\\\'}), is_leaf=True, yang_name="id", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True, is_keyval=True, namespace=\\\'<MY_NAMESPACE>\\\', defining_module=\\\'<MY_MODULE>\\\', yang_type=\\\'id-type\\\', is_config=True)\'}'

Do you know any work-around for this problem? BTW, I'm using pyangbind version 0.8.5.

Thank you Voica

JoseIgnacioTamayo commented 3 months ago

Interesting, when Decoding a JSON blob for the same model, it all works fine.

The XML is valid, I get to encode the same text from the binding.

Could you try using JSON instead of XML, for now?

JoseIgnacioTamayo commented 3 months ago

I was able to narrow this down to pyanbind trying to check the pattern restriction on an lxml.objectify.StringElement object, which is not a native string type. With JSON decoding, this problem does not happen because the JSON value is a native str type.

So in https://github.com/robshakir/pyangbind/tree/fix_xml_decode_regex_key I added a UnitTest and a fix for this.

Could you please try to use this Branch and let me know if this works for you. I will raise a PR after that.

CC-vga commented 3 months ago

Hi,

Thanks, I’ll take the branch suggested 😊 From what I saw, I need to strip of escaped ‘ the value of the string primary key.

All best

From: Jose Ignacio Tamayo Segarra @.> Sent: Wednesday, 13 March 2024 10:41 pm To: robshakir/pyangbind @.> Cc: Voica Gavrilut @.>; Author @.> Subject: Re: [robshakir/pyangbind] Primary key of a list cannot be of type string with a specific pattern (Issue #345)

[External]

I was able to narrow this down to pyanbind trying to check the pattern restriction on an lxml.objectify.StringElement object, which is not a native string type. With JSON decoding, this problem does not happen because the JSON value is a native str type.

So in https://github.com/robshakir/pyangbind/tree/fix_xml_decode_regex_key I added a UnitTest and a fix for this.

Could you please try to use this Branch and let me know if this works for you. I will raise a PR after that.

— Reply to this email directly, view it on GitHubhttps://github.com/robshakir/pyangbind/issues/345#issuecomment-1995912072, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AMLNYJUKDEC2RZEET4XIPTTYYDBWNAVCNFSM6AAAAABEK3Z5EGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSOJVHEYTEMBXGI. You are receiving this because you authored the thread.Message ID: @.**@.>>

CC-vga commented 3 months ago

Hi again,

I’m sorry, I didn’t manage to install the package from the given branch!

Cheers

From: Jose Ignacio Tamayo Segarra @.> Sent: Wednesday, 13 March 2024 10:41 pm To: robshakir/pyangbind @.> Cc: Voica Gavrilut @.>; Author @.> Subject: Re: [robshakir/pyangbind] Primary key of a list cannot be of type string with a specific pattern (Issue #345)

[External]

I was able to narrow this down to pyanbind trying to check the pattern restriction on an lxml.objectify.StringElement object, which is not a native string type. With JSON decoding, this problem does not happen because the JSON value is a native str type.

So in https://github.com/robshakir/pyangbind/tree/fix_xml_decode_regex_key I added a UnitTest and a fix for this.

Could you please try to use this Branch and let me know if this works for you. I will raise a PR after that.

— Reply to this email directly, view it on GitHubhttps://github.com/robshakir/pyangbind/issues/345#issuecomment-1995912072, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AMLNYJUKDEC2RZEET4XIPTTYYDBWNAVCNFSM6AAAAABEK3Z5EGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSOJVHEYTEMBXGI. You are receiving this because you authored the thread.Message ID: @.**@.>>

JoseIgnacioTamayo commented 3 months ago

From what I saw, I need to strip of escaped ‘ the value of the string primary key.

What do you mean? By editing somethin on the XML? I tried scaping, and the issue I found is specifically to Decoding XML which produce a non-str type object.

I’m sorry, I didn’t manage to install the package from the given branch!

You need to git clone locally the branch, instead of using the package from pip. Something like:

git clone git@github.com:robshakir/pyangbind.git
cd pyangbind
git checkout fix_xml_decode_regex_key
PYTHONPATH=. pyang --plugindir ./pyangbind/plugin -f pybind -o binding.py <hier your YANG file>
PYTHONPATH=. py # here do the decoding of your XML
JoseIgnacioTamayo commented 2 months ago

Considering this fixed in https://github.com/robshakir/pyangbind/pull/346