Opened 11 years ago

Closed 11 years ago

#6756 closed defect (wontfix)

JSON Procedure Call with named parameters

Reported by: guest Owned by: Dustin Machi
Priority: high Milestone:
Component: RPC Version: 1.1.0
Keywords: RPC, JSON, named parameters Cc: alex
Blocked By: Blocking:

Description (last modified by alex)

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
  1. 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 (3)

dojo.response.txt (4.4 KB) - added by guest 11 years ago.
details of error response
RpcService.js (5.5 KB) - added by guest 11 years ago.
definition.smd (732 bytes) - added by guest 11 years ago.

Download all attachments as: .zip

Change History (8)

Changed 11 years ago by guest

Attachment: dojo.response.txt added

details of error response

comment:1 Changed 11 years ago by alex

Description: modified (diff)

comment:2 Changed 11 years ago by alex

Description: modified (diff)

Changed 11 years ago by guest

Attachment: RpcService.js added

Changed 11 years ago by guest

Attachment: definition.smd added

comment:3 Changed 11 years 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

comment:4 Changed 11 years 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;
}

comment:5 Changed 11 years ago by Dustin Machi

Resolution: wontfix
Status: newclosed

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.