Ticket #6756 (closed defect: wontfix)

Opened 2 months ago

Last modified 3 weeks ago

JSON Procedure Call with named parameters

Reported by: guest Owned by: dmachi
Priority: normal Milestone:
Component: RPC Version: 1.1.0
Severity: normal Keywords: RPC, JSON, named parameters
Cc: alex

Description (last modified by alex) (diff)

It is not possible to make JSON RPC with named parameters.

Example:

       var myObject = new dojo.rpc.JsonService("/definition.smd");
       var myDeferred = myObject.add(121,5});
       myDeferred.addCallback(myCallbackMethod);
       myDeferred.addErrback(myErrorbackMethod);

add method definition:

methods:[
{
	name: "add",
	parameters: {
	{
		name: "x",
		type: "integer"
	},
	{
		name: "y",
		type: "integer"
	}
}

Expected: params = {x:131,y:5}

Result:

[ Error: invalid property id ]
[ Error: Unable to load SMD from https://www.somewebsite.org/definition.smd ]

Details:

According to http://groups.google.com/group/json-rpc/web/simple-method-description-for-json-rpc-example if parameters are given as object than it indicates that named parameters should be used for this method.

It is not possible to give parameters as object. If definition smd is changed to:

methods:[
{
	name: "add",
	parameters: [
	{
		name: "x",
		type: "integer"
	},
	{
		name: "y",
		type: "integer"
	]
}

submitted parameters are {131,5}

Even in case that it would be possible to load smd with parameters defined as object on line 136 of dojo/rpc/RpcService.js it would not be taken in account but given runtime arguments would be converted to array and submitted as parameters.

Resolution:

1. Enable parsing smd with parameters given as objec

2. Follow JSON-RPC (over HTTP) specification/example and correctly pass parameters as position or named.

I could not find out why smd is not parsed (I am new to JavaScript?) but once that is resolved than something like this could be done: add method:

 	toNamed: function(args, param) {
		var i;
		var data = {};
		for (i=0; i<param.length; i++) {
			data[param[i].name] = args[i];
		}
		return data;
	}

and than on line 136 do something like this:

				if (true === dojo.isObject(parameters)) {
					this.bind(method, this.toNamed(dojo._toArray(arguments), parameters), deferredRequestHandler, url);
				} else {
					this.bind(method, dojo._toArray(arguments), deferredRequestHandler, url);
}

Attachments

dojo.response.txt (4.4 kB) - added by guest 2 months ago.
details of error response
RpcService.js (5.5 kB) - added by guest 2 months ago.
definition.smd (0.7 kB) - added by guest 2 months ago.

Change History

Changed 2 months ago by guest

details of error response

Changed 2 months ago by alex

  • description modified (diff)

Changed 2 months ago by alex

  • description modified (diff)

Changed 2 months ago by guest

Changed 2 months ago by guest

Changed 2 months ago by guest

I've made several changes to RpcService?.js so that smd which follows JSON RPC over HTML would be parsed correctly. Parameters would be sent as positioned if defined as array or as named if defined as object. See: http://groups.google.com/group/json-rpc/web/simple-method-description-for-json-rpc-example (see attached files RpcService?.js and definition.smd)

If needed I can supply entire example. There are several problems in given PHP example but since it's not strictly dojo problem and I am not sure should I submit it as a bug or just post comment on http://dojotoolkit.org/book/dojo-book-0-9/part-3-programmatic-dijit-and-dojo/ajax-transports/remote-procedure-call-rpc#comment-15290

Changed 2 months ago by guest

Switch on line 132 in attached RpcService?.js should be changed to test if parameters are array. :( I know Array in JavaScript? is object but I've expected that dojo.isObject() would handle it correctly.

switch (true) {
	case dojo.isArray(parameters):
		if( (this.strictArgChecks) &&
			(parameters != null) &&
			(arguments.length != parameters.length)
		){
			// put error stuff here, no enough params
			throw new Error("Invalid number of parameters for remote method.");
		}
		param = dojo._toArray(arguments);
		break;

	default:
		param = this.toNamed(dojo._toArray(arguments), parameters)
		break;
}

Changed 3 weeks ago by dmachi

  • status changed from new to closed
  • resolution set to wontfix

The newer SMD definitions and JSON-RPC are implemented in dojox.rpc.Service, the current dojo.rpc.RpcService? and its children use the original 0.1 smd format and only work with the first version of JSON-RPC.

Note: See TracTickets for help on using tickets.