etingof / pyasn1

Generic ASN.1 library for Python
http://snmplabs.com/pyasn1
BSD 2-Clause "Simplified" License
244 stars 118 forks source link

Unwanted optional property is added while encoding. #186

Open daniel-leicht opened 4 years ago

daniel-leicht commented 4 years ago

Hello dear pyasn1 maintainers,

I managed to solved some issues in my journey into using pyasn1 for MMS so far, but I hit a wall on this one:

Decoding seems fine, but when I'm trying to encode an object that has an optional property, the property is suddenly added to the object even tho I did not specify it. For example, the property "listOfModifier" in the example bellow:

mms_pdu = MMSpdu()
confirmed_request = mms_pdu['confirmed-RequestPDU']
confirmed_request['invokeID'] = invoke_id_counter
invoke_id_counter += 1
confirmed_service_request = confirmed_request['confirmedServiceRequest']
confirmed_service_request['identify'] = None
print(mms_pdu)
print(decode(encode(mms_pdu, schema=MMSpdu), asn1Spec=MMSpdu().subtype())[0])

The printed result is:

MMSpdu:
 confirmed-RequestPDU=Confirmed_RequestPDU:
  invokeID=0
  confirmedServiceRequest=ConfirmedServiceRequest:
   identify=

MMSpdu:
 confirmed-RequestPDU=Confirmed_RequestPDU:
  invokeID=0
  listOfModifier=SequenceOf:

  confirmedServiceRequest=ConfirmedServiceRequest:
   identify=

Definition files used: https://pastebin.com/JitDfL2R (named it mms_classes.py)

ptitdoc commented 3 years ago

https://github.com/etingof/pyasn1/blob/db8f1a7930c6b5826357646746337dafc983f953/pyasn1/codec/ber/encoder.py#L531

https://github.com/etingof/pyasn1/pull/162

The commit message says

- Removed default initializer from `SequenceOf`/`SetOf` types to ensure
  consistent behaviour with the rest of ASN.1 types. Before this change,
  `SequenceOf`/`SetOf` instances immediately become value objects
  behaving like an empty list. With this change, `SequenceOf`/`SetOf`
  objects remain schema objects unless a component is added or
  `.clear()` is called.

In my case, it seems that I still have objects behaving as lists even when empty.

It possible to troubleshoot this issue by enabling debugging in your code:

from pyasn1 import debug
debug.setLogger(debug.Debug('encoder'))