strongloop / strong-remoting

Communicate between objects in servers, mobile apps, and other servers.
www.strongloop.com
Other
105 stars 93 forks source link

Add support for coercion by endpoints with type File #318

Closed devInterests closed 7 years ago

devInterests commented 8 years ago

If you have a remote method with a parameter that's type: 'file' you receive following error: "Error: Invalid value for argument 'file' of type 'file': [object Object]. Received type was converted to object.

As far as I understood the error is in function convertToBasicRemotingType in shared-method.js:

function convertToBasicRemotingType(type) {
  if (Array.isArray(type)) {
    return type.map(convertToBasicRemotingType);
  }
  if (typeof type === 'object') {
    type = type.modelName || type.name;
  }

  type = String(type).toLowerCase();

  switch (type) {
    case 'string':
    case 'number':
    case 'date':
    case 'boolean':
    case 'buffer':
    case 'object':
    case 'file':
    case 'any':
      return type;
    case 'array':
      return ['any'].map(convertToBasicRemotingType);
    default:
      // custom types like MyModel
      return 'object';
  }
}

The type file is supported as a separate type. The problem is that the arg is coming as type object. My fix at the moment is to use object as arg type or to use a older version where the file type is not supported and becomes default type object. But i would like to use the correct type: file.

My interface definition:

model.remoteMethod( 
       'fileUpload',
        {
            http:{ verb:'Post', source: 'body', path: '/upload/:id'},
            description: '',
            accepts: [
                {arg: 'id', type: 'string', http: { source: 'path' }, required: true},
                {arg: 'file', type: 'file', http: { source: 'body' }, required: true},
                {arg: 'req', type: 'object', http: {source: 'req'}}
            ],
            returns: {arg: 'res', type: 'object', root:true}
        });

Perhaps I am doing something wrong?

richardpringle commented 8 years ago

Hey @devInterests sorry about the delayed response.

The accepts.type must a LoopBack type.

I am going to close this issue for now but please feel free to carry on the conversation in here or re-open if you believe this is still an issue.

devInterests commented 8 years ago

Hello,

@richardpringle I was aware of the list. It seems I misunderstood this answer: Uploading images

  1. We allow remote method parameters to be typed as ‘File’. If one of the parameters is typed as ‘File’, only ‘multipart/form-data’ content type is supported.

It sounds to me like the File-Type is supported.

richardpringle commented 8 years ago

@devInterests, the idea is that type "File" will eventually be supported.

Does your fileUpload method work at all? If not, I suggest either using loopback-component-storage, or go take a look at the code so you can implement your own.

The storage-service code is where you'll find the remote method and its metadata. The upload method actually call handler.upload which can be found in the storage-handler file.

I think it would be easiest to just add loopback-component-storage as a datasource service without exposing the container to the public REST API.

Keep the conversation going in here and we'll get your method working the way you want it to!

devInterests commented 8 years ago

@richardpringle thanks. My file upload works just fine, when I am using object as a Argument-Type. The problem here is that the Swagger-UI treats it as a object.

When I am using File as a Argument-Type, Swagger-UI treats it as a file upload. It is showing Browser-Button to select a File, and a Upload-Button. But then loopback get the error.

I would like to have the right definition with the File-Type, because then Swagger showing the Rest-Interface correctly (which is part of the rest documentation) and also I can test with Swagger directly.

richardpringle commented 8 years ago

Interesting,

I am going to re-open this issue as a feature request then.

@devInterests, would you be interested in submitting a PR to add support of type File?

richardpringle commented 8 years ago

@devInterests, what version of strong-remoting do you have? I just came across this issue that should support type File: https://github.com/strongloop/loopback/issues/2063#issuecomment-189204577

Edit: Sorry, that's actually only supported for returns

bajtos commented 8 years ago

Closing as a duplicate of https://github.com/strongloop/loopback/issues/2554

bajtos commented 8 years ago

Ooops, this is not a duplicate of strongloop/loopback#2554, reopening.

@devInterests if I understand your comments correctly, then you would like strong-remoting to support {arg: 'file', type: 'file', http: { source: 'body' }, required: true}, is that correct?

bajtos commented 7 years ago

Closing as a duplicate of https://github.com/strongloop/loopback/issues/3266