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

Add support for file type #34 #90

Merged
merged 1 commit into from
Aug 3, 2017

Conversation

zbarbuto
Copy link
Member

Also closes strongloop/loopback-component-explorer#103

Description

Allows File type to be turned into a file upload field.

Related issues

Checklist

  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style
    guide

@slnode
Copy link

slnode commented Jul 18, 2017

Can one of the admins verify this patch? To accept patch and trigger a build add comment ".ok\W+to\W+test."

@slnode
Copy link

slnode commented Jul 18, 2017

Can one of the admins verify this patch?

3 similar comments
@slnode
Copy link

slnode commented Jul 18, 2017

Can one of the admins verify this patch?

@slnode
Copy link

slnode commented Jul 18, 2017

Can one of the admins verify this patch?

@slnode
Copy link

slnode commented Jul 18, 2017

Can one of the admins verify this patch?

@bajtos bajtos self-assigned this Jul 18, 2017
@bajtos bajtos added the feature label Jul 18, 2017
@bajtos
Copy link
Member

bajtos commented Jul 18, 2017

@slnode ok to test

@bajtos
Copy link
Member

bajtos commented Jul 18, 2017

Hello @zbarbuto, thank you for the pull request! The changes looks mostly good to me, however I think we need to be more careful about the new type name.

TL;DR: let's use file as the type name please.

Background: see the discussion in strongloop/loopback#2554. As of strongloop/strong-remoting#370, only file is recognized as a type representing a file (arbitrary stream). The name File is left available for an application-defined model.

@zbarbuto
Copy link
Member Author

zbarbuto commented Jul 18, 2017

@bajtos Fair enough. Updated.

The name File is left available for an application-defined model.

Interesting. I thought this wasn't the case as the model name gets lower-cased loopback throws an error if the application tries to define a model called 'file' or 'File':
strongloop/strong-remoting@2c4cc6e

Or by 'application-defined' do you mean core to loopback?

@@ -141,11 +141,6 @@ function convertApi(v2, apiDeclaration, definitions) {
} else {
_.extend(target, props);
}
// Files hasn't made it over yet.
//
if (target.type === 'File') {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bajtos Also removed this bock as doesn't seem to be the case anymore after this PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, makes sense. Just note that AFAIK, strong-remoting does not support file uploads (input parameters of file type) yet - see strongloop/loopback#3266

@bajtos
Copy link
Member

bajtos commented Jul 19, 2017

The name File is left available for an application-defined model.

Interesting. I thought this wasn't the case as the model name gets lower-cased loopback throws an error if the application tries to define a model called 'file' or 'File':
strongloop/strong-remoting@2c4cc6e

Or by 'application-defined' do you mean core to loopback?

By application-defined I mean a model defined by a 3rd-party/user application using LoopBack as the framework. E.g. https://github.com/strongloop/loopback-sandbox (it does not have any File model defined though).

After I looked around in strong-remoting, I am little bit confused about the benefits of your proposed changes TBH. If strong-remoting which treats file as a built-in/reserved type that's not supported for input arguments yet, then what's the point of supporting input parameters with file type in swagger spec? What am I missing?

@@ -26,6 +26,7 @@ function TypeRegistry() {
},
});
this.registerLoopbackType('DateString', {type: 'string', format: 'date-time'});
this.registerLoopbackType('File', {type: 'file'});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the type name should be file for consistency with other places where we are using file too.

@@ -103,6 +103,20 @@ describe('route-helper', function() {
});
});

it('converts { type: File\' } to { schema: { type: \'file\' } }', function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto, type: file

var TestModel = loopback.createModel('TestModel', {street: String});
var entry = createAPIDoc({
accepts: [
{name: 'changes', type: 'File'},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here too

@zbarbuto
Copy link
Member Author

zbarbuto commented Jul 19, 2017

After I looked around in strong-remoting, I am little bit confused about the benefits of your proposed changes TBH. If strong-remoting which treats file as a built-in/reserved type that's not supported for input arguments yet, then what's the point of supporting input parameters with file type in swagger spec? What am I missing?

The crux of it is: I want to be able to use the api explorer in conjunction with https://github.com/strongloop/loopback-component-storage in order to test file upload endpoints.

At the moment, I'm having to use third-party API tools just to test my file upload endpoints. Since swagger definitely supports file uploads I thought it would just be a case of modifying the swagger gen to actually output a file upload type in the event of an accepts: type: file situation, then allow swagger to take it from there.

In short: the purpose of this pull request is to allow us to get a file input field in our API explorer instead of just strings as are allowed now.

@bajtos
Copy link
Member

bajtos commented Aug 2, 2017

@zbarbuto first of all, sorry for the delay.

The crux of it is: I want to be able to use the api explorer in conjunction with https://github.com/strongloop/loopback-component-storage in order to test file upload endpoints.

That makes sense 👍

However, I think the fix will require more changes than just here in loopback-swagger.

Take a look at how the file upload method is defined in lib/storage-service.js:

StorageService.prototype.upload.shared = true;
StorageService.prototype.upload.accepts = [
  {arg: 'req', type: 'object', 'http': {source: 'req'}},
  {arg: 'res', type: 'object', 'http': {source: 'res'}},
];
StorageService.prototype.upload.returns = {arg: 'result', type: 'object'};
StorageService.prototype.upload.http =
{verb: 'post', path: '/:container/upload'};

It is accepting a full request object, there is no mention of file uploads. As a result, strong-remoting and loopback-swagger don't have any visibility into what kind of data is the method expecting, we cannot tell that this method expect file uploads.

The proper solution is to implement support for file uploads in strong-remoting and then rework loopback-component-storage to let strong-remoting handle file uploads, instead of the current implementation where loopback-component-storage is parsing multipart requests itself (see lib/storage-handler.js). With that two changes in place, and this pull request landed, you should finally be able to upload files directly from our swagger-ui powered Explorer.

Thoughts?

@zbarbuto
Copy link
Member Author

zbarbuto commented Aug 2, 2017

@bajtos you're correct in that it would take multiple changes to get it to work with storage component out of the box. However in my case I'm not using the default methods provided by storage component. I've wrapped them in a custom endpoint. So what this PR achieves is allowing arbitrary endpoints to specify a file type for swagger. Then a separate issue/PR could be made for storage to support this by default.

@bajtos
Copy link
Member

bajtos commented Aug 3, 2017

So what this PR achieves is allowing arbitrary endpoints to specify a file type for swagger. Then a separate issue/PR could be made for storage to support this by default.

Sure, makes sense.

@bajtos bajtos merged commit 5292530 into strongloop:master Aug 3, 2017
@bajtos
Copy link
Member

bajtos commented Aug 3, 2017

I have rebased you patch on top of the latest master and squashed the commits into a single one.

Landed, thank you for the contribution!

@bajtos
Copy link
Member

bajtos commented Aug 10, 2017

For posterity, strongloop/loopback#3266 is keeping track of the feature allowing remote method to accept file uploads.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

File upload feature in explorer (nice to have)
3 participants