loopback-connector-swagger icon indicating copy to clipboard operation
loopback-connector-swagger copied to clipboard

[Question / Need Support] loopback-connector-swagger is not exposing any method.

Open mercuriete opened this issue 10 years ago • 7 comments

I need to create a kind of gateway that conects to another REST endpoint defined by swagger.

so i tried to do the same that i did with loopback-connector-soap examples.

now i have this:

var ds = loopback.createDataSource('swagger', { connector: 'loopback-connector-swagger', spec: 'swagger/ees/swagger-file.json', validate: true });

ds.once('connected', function () {

// Create the model var Service = ds.createModel('Service', {}); // Expose to REST app.model(Service);

//some more code below

}

My question is... I supposed that this connector automatically parse the swagger and then exposes all the method that are defined in that swagger.

I am suposing something wrong?

Do I have to implements all wrappers to all methods?

Thanks in advance.

mercuriete avatar Aug 11 '16 11:08 mercuriete

@mercuriete : Your assumption:

I supposed that this connector automatically parse the swagger and then exposes all the method that are defined in that swagger.

is correct. The way connector works is: it parses the swagger spec you provided and generates methods automatically. Those methods are available as model methods when a model is attached to the swagger datasource.

In your example, Service model will have all the operations defined in swagger/ees/swagger-file.json spec and you can call, for example an operation: getServiceTag as, Service.getServiceTag().

Hope that answers your question.

gunjpan avatar Aug 18 '16 15:08 gunjpan

@mercuriete : Closing this for now. Should you need further assistance with it, please mention me in the comments and I'll be happy to help. Thank you & have a great day.

gunjpan avatar Aug 24 '16 13:08 gunjpan

@gunjpan sorry i didn't notice that you answer me. Thanks for your quick answer. Maybe i need to create a small project to show what happened to me. when I create the model, using a debugger i realize that there are a client created. but with stronloop explorer the model is shown but without any methods.

So my question was.. if i have to create all methods wrapping this client.

I suposed that all methods was automatically created like in connector-soap.

I will try to create a small example of use in order to show my problem.

Thank you very much :+1:

PS: My use case is create a gateway without logic that redirect rest messages to another rest backend.

mercuriete avatar Aug 24 '16 18:08 mercuriete

@mercuriete : Exposing client methods as model methods is not yet implemented. It's on the road map.

For time being, you may need to create remote methods that wrap around the swagger API methods and expose them as REST end points for a model.

gunjpan avatar Aug 24 '16 18:08 gunjpan

thank you very much for the support. I am already got working an example using your advice.

sorry for not anwser quickly.

Thank you for this project :smile: :+1:

mercuriete avatar Sep 15 '16 08:09 mercuriete

@mercuriete : Glad it worked for you! :)

gunjpan avatar Sep 28 '16 15:09 gunjpan

Just to add my 2 cents: the Swagger endpoint returns JSON. When you wrap the internal call with a remote method, you must parse the result:

var result = JSON.parse(res.data);
cb(null, result);

Otherwise, you'll get a double encoded string with escaped double quotes.

ffflabs avatar Oct 26 '18 21:10 ffflabs