Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json deserialization we should not add "/" when missing #651

Closed
sbernard31 opened this issue Feb 6, 2019 · 4 comments
Closed

Json deserialization we should not add "/" when missing #651

sbernard31 opened this issue Feb 6, 2019 · 4 comments
Labels
bsserver Impact LWM2M bootstrap server bug Dysfunctionnal behavior client Impact LWM2M client server Impact LWM2M server

Comments

@sbernard31
Copy link
Contributor

It seems we are too "smart" and we should not add "/" in json path.
(see OpenMobileAlliance/OMA_LwM2M_for_Developers#380)

@sbernard31 sbernard31 changed the title Json deserialization we should not add "/" Json deserialization we should not add "/" when missing Feb 6, 2019
@sbernard31 sbernard31 added bug Dysfunctionnal behavior client Impact LWM2M client server Impact LWM2M server bsserver Impact LWM2M bootstrap server labels Feb 6, 2019
@sbernard31
Copy link
Contributor Author

sbernard31 commented Oct 25, 2019

The issues :

  • e field is mandatory
  • n field is optional
  • at least one of the value fields (v, bv, ov, sv) must be present.
  • bn+n is a simple string concatenation (we must not add missing "/" in path)
  • we MUST NOT use request path as base name if base name is absent !

@sbernard31
Copy link
Contributor Author

This is fixed since #762 is integrated in master

@ezhaboy
Copy link

ezhaboy commented Jun 20, 2021

@sbernard31

Hi Simon

I'm evaluating possibility of upgrading Leshan to higher version for my project. I see there was a difference between 1.0.0-M12 and 1.0.0-M13, which was introduced by this issue.

Since Leshan 1.0.0-M13, there is explicit declaration in LwM2M JSON decoder, as I copy/paste below. Decoder requires that LwM2M Resource Path of JSON payload MUST as the same as request path. Would be appreciate if you can share some background about this issue or reason behind?

     // check returned path is under requested path
    if (requestPath.getObjectId() != null && path.getObjectId() != null) {
        if (!path.getObjectId().equals(requestPath.getObjectId())) {
            throw new CodecException("resource path [%s] does not match requested path [%s].", path, requestPath);
        }
@Test
public void working_test_case_before_m13() throws CodecException {
    // json content for instance 0 of device object
    StringBuilder b = new StringBuilder();
    b.append("{");
    b.append("\"e\":[");
    b.append("{\"n\":\"\", \"sv\":\"Open Mobile Alliance\"}]}");

    LwM2mResource oInstance = (LwM2mResource) decoder.decode(b.toString().getBytes(), ContentFormat.JSON,
            new LwM2mPath(3, 0, 1), model);
}

@sbernard31
Copy link
Contributor Author

Hi,

I strongly advice to migrate to last 1.x version. (currently v1.3.1)
This branch is stable, receives bug/security fixes and has stable API meaning you should upgrade it.

Using Milestone version only makes sense if you need features which are not yet available in the stable version. (like using 2.0.0-M? to get LWM2M v1.1 features)
Using old Milestones version have no benefits and many drawbacks.

Anyway back to your question,

Decoder requires that LwM2M Resource Path of JSON payload MUST as the same as request path. Would be appreciate if you can share some background about this issue or reason behind?

I don't know what I can explain more than what is already explained in comment above 🤔 :

In particular : "we MUST NOT use request path as base name if base name is absent !"

About this test working_test_case_before_m13, the issue that your JSON is malformed as bn+n is not a valid path under the requested one (/3/0/1). In this case bn+n="" but should be bn+n = "/3/0/1".

Each entry of the JSON format is a Resource Instance, where the Name attribute need to be prepended by the Base Name attribute to form the unique identifier of this Resource instance.The Name attribute in of this array entry is a URI path relative to the Base Name; namely the Name attribute could simply be the full URI of a requested Resource Instance when Base Name is absent.

(source lwm2m-v1.0.2§6.4.4-JSOn)

// check returned path is under requested path
if (requestPath.getObjectId() != null && path.getObjectId() != null) {
    if (!path.getObjectId().equals(requestPath.getObjectId())) {
        throw new CodecException("resource path [%s] does not match requested path [%s].", path, requestPath);
    }
}

The idea of this check is to ensure that JSON value only contains resource which is under the requested path.
(e.g. check that we don't find a "/1/0/1" when we request "/3/0")

If you face some devices which do not respect the specification you can eventually create a custom JSON encoder/decoder. (of course better is to fix devices but not always possible :-/)
See :

  • DefaultLwM2mNodeDecoder
  • DefaultLwM2mNodeEncoder
  • LeshanServerBuilder.setEncoder
  • LeshanServerBuilder.setDecoder

HTH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bsserver Impact LWM2M bootstrap server bug Dysfunctionnal behavior client Impact LWM2M client server Impact LWM2M server
Projects
None yet
Development

No branches or pull requests

2 participants