open-coap / java-coap

CoAP Java library
Apache License 2.0
18 stars 5 forks source link

Add support of Option without value in Uri-Query. #45

Closed sbernard31 closed 1 year ago

sbernard31 commented 1 year ago

coapRequest.options().getUriQueryMap() seems to only support Option in the form of a "key=value" pair. It should also support more simple form like "key" without value.

(See more detailed at : https://github.com/open-coap/java-coap/issues/27#issuecomment-1507287371)

szysas commented 1 year ago

Currently empty query value can be placed in such way: field1=value1&field2=&field3=, so equal sign is needed. Would that work with Leshan?

sbernard31 commented 1 year ago

Nope that does not match the LWM2M specification :grimacing:

See for Q option : /rd?ep={Endpoint Client Name}&lt={Lifetime}&lwm2m={version}&b={binding}&Q&sms={MSISDN}

sbernard31 commented 1 year ago

Currently I parse UriQuery by myself using :

// TODO remove this class when https://github.com/open-coap/java-coap/issues/45 will be fixed.
public class QueryUtil {
    public static Map<String, String> parseUriQuery(String uriQuery) {
        if (uriQuery == null || uriQuery.length() == 0) {
            return Collections.emptyMap();
        }
        Map<String, String> result = new LinkedHashMap<>();
        String[] params = uriQuery.substring(uriQuery.indexOf('?') + 1).split("&");

        for (String prm : params) {
            String[] p = prm.split("=", 2);
            if (p.length == 2) {
                result.put(p[0], p[1]);
            } else if (p.length == 1) {
                result.put(p[0], null);
            } else {
                throw new IllegalArgumentException();
            }
        }
        return result;
    }
}
for (Entry<String, String> entry : QueryUtil.parseUriQuery(coapRequest.options().getUriQuery())
                    .entrySet()) {
 ... .. 
}
szysas commented 1 year ago

There is release v6.12.0 that contains implementation for this ticket, can we close it?

sbernard31 commented 1 year ago

Tested with v6.12.0, my integrations tests pass now without my "HACK" :heavy_check_mark:

Thx for adding this :pray: