Is there a JS class generator for Swagger

1,931 views
Skip to first unread message

Troy Forster

unread,
Feb 8, 2015, 5:53:05 PM2/8/15
to swagger-sw...@googlegroups.com
It appears my previous post did not make it.

Does anyone know if a lib that allows the creation of an empty JS class from a Swagger schema? There are numerous client generators but they presume an existing server. I am writing a NodeJS REST server and really wanted to take advantage of Swagger to define and document the API as well as some of the internal code generation. 

Given a schema definition in Swagger I would like to simply create a new empty JavaScript object on demand for which I can dynamically set various properties before returning to the calling client.

/troy

tony tam

unread,
Feb 10, 2015, 11:46:53 PM2/10/15
to swagger-sw...@googlegroups.com
Hi, seems like your post got stuck in spam.  Yes of course, there is a swagger-js project dedicated to this, and the swagger-ui uses it almost exclusively.

Check out the project here:


It's a very rich client.

Troy Forster

unread,
Feb 11, 2015, 8:45:46 AM2/11/15
to swagger-sw...@googlegroups.com
Thanks Tony,

Perhaps you could provide an example? I have already gone through swagger-js and I don't see how it can be applied to my scenario. For instance, having my server make a roundtrip http request to itself to create an instance on just about every inbound request seems like an awful lot of overhead to me. 

var swagger = new client.SwaggerClient({
  url: 'http://petstore.swagger.wordnik.com/v2/swagger.json',

I have looked through the source and I don't see any facility for loading schema from disk and/or caching schema. 

As I mentioned I am not looking for a client generator to run against someone else's server. I need to create classes on my own server. It seems onerous to build these classes in hard code when I have a swagger json file available.

/troy

TROY FORSTER CREATIVE TECHNOLOGIST

+1 (416) 617-4921 (mobile - text me for best results)


Consider our environment, think before you print

--
You received this message because you are subscribed to a topic in the Google Groups "Swagger" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/swagger-swaggersocket/OH18COyX7-s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ron Ratovsky

unread,
Feb 11, 2015, 8:49:07 AM2/11/15
to swagger-sw...@googlegroups.com
With https://github.com/swagger-api/swagger-js/issues/139 you can pass the spec as a JSON object to the SwaggerClient object using the 'spec' variable (instead of 'url).

Does that help?

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
-----------------------------------------
http://swagger.io
https://twitter.com/SwaggerApi
-----------------------------------------

Troy Forster

unread,
Feb 11, 2015, 8:51:34 AM2/11/15
to swagger-sw...@googlegroups.com
Ron that sounds like what I am looking for, thanks. I will investigate that tonight.

Cheers,

/troy

TROY FORSTER CREATIVE TECHNOLOGIST

+1 (416) 617-4921 (mobile - text me for best results)


Consider our environment, think before you print

Troy Forster

unread,
Mar 14, 2015, 5:59:24 PM3/14/15
to swagger-sw...@googlegroups.com
Ron,

Thanks for your help. I finally got around to testing this today and unfortunately it does not work. I get a 404. I think the example provided
swagger.apis.pet.getPetById({petId:1});
is assuming that an API endpoint exists that will service the call. Perhaps I can readdress my original need/requirement.

Let's assume I have a very simple schema with a definition of 
    "Milestone": {
         "properties": {
            "name": {
               "type": "string",
               "description": "Name or type of milestone. Currently assumed to be 'Substantial Completion'."
            },
            "date": {
               "type": "string",
               "format": "date",
               "description": "Date the milestone becomes due."
            }
         }
      },

Somewhere in my code there is a need to create a new milestone. I would like to be able to 

var myMilestone = new emptyObjectFromSomeSwaggerSchema(Milestone) 

and get back

{
   "myMilestone": {
      "name": "",
      "date": "2015-03-14"
   }
}

Then I can modify the properties according to my needs and act on the object such as saving it to the db or sending it over the wire in response to an API request.

However, it seems that I can't do this and instead I have to build each object manually in code. While this is trivial for the example given it would be onerous for the complex object schemas that I need. And, every time I deviate as part of the iterative dev-test-dev process I have to remember to update both the swagger schema and my code. 

There must be some way to obtain an empty data object from a Swagger definition?

Hopeful,

Troy

TROY FORSTER CREATIVE TECHNOLOGIST

+1 (416) 617-4921 (mobile - text me for best results)


Consider our environment, think before you print

On Wed, Feb 11, 2015 at 8:49 AM, Ron Ratovsky <r...@swagger.io> wrote:

Jimmy Pannier

unread,
Dec 4, 2015, 5:17:36 AM12/4/15
to Swagger
Hi. Do you resolve the issue ?

Can you create parameters with schema definition ? I'd like to have an empty parameters too  that converts into json while sending (and for response too) !
To unsubscribe from this group and all its topics, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
-----------------------------------------
http://swagger.io
https://twitter.com/SwaggerApi
-----------------------------------------

--
You received this message because you are subscribed to a topic in the Google Groups "Swagger" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/swagger-swaggersocket/OH18COyX7-s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

Troy Forster

unread,
Dec 4, 2015, 6:25:37 PM12/4/15
to swagger-sw...@googlegroups.com, Swagger
I don't have code in front of me right now but it can be done with latest swagger for node. However, the way I got it to work increased processing time by 10x. Servicing an API call went from approx 20ms to 200ms. I have to refactor to see if I can get back to 20ms and post snippets. 

Sent from a small silicon based life-form


To unsubscribe from this group and all its topics, send an email to swagger-swaggers...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
-----------------------------------------
http://swagger.io
https://twitter.com/SwaggerApi
-----------------------------------------

--
You received this message because you are subscribed to a topic in the Google Groups "Swagger" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/swagger-swaggersocket/OH18COyX7-s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to swagger-swaggers...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Swagger" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/swagger-swaggersocket/OH18COyX7-s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to swagger-swaggers...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages