Generate Swagger spec from persistent and serial classes
Recently I needed to generate a Swagger spec from persistent and serial classes, so I'm publishing my code (it's not complete - you still need to hash out the application specifics, but it's a start). It's available here.
Let's say you have these classes:
Classes
You can automatically generate this Swagger definition from them:
REST.Test.Person:
type: "object"
properties:
Age:
type: "integer"
DOB:
type: "string"
FavoriteColors:
type: "array"
items:
type: "string"
FavoriteNumbers:
type: "object"
Home:
$ref: "#/definitions/REST.Test.Address"
Name:
type: "string"
Office:
$ref: "#/definitions/REST.Test.Address"
SSN:
type: "string"
Spouse:
$ref: "#/definitions/REST.Test.Person"
REST.Test.Address:
type: "object"
properties:
City:
type: "string"
State:
type: "string"
Street:
type: "string"
Zip:
type: "string"Main method: Utils.YAML:GenerateClasses
Test run: do ##class(Utils.YAML).Test()
Comments
Hi Eduard.
Awesome job, congratulations!
Is there something similar but for REST Dispatcher classes?
Can you elaborate a bit?
What dispatcher class do you want generated?
Sure! Let's say I have this dispatcher class:
XData UrlMap [ XMLNamespace = "http://www.intersystems.com/urlmap" ]
{
<Routes>
<Route Url="/pets/:id" Method="delete" Call="deletePet" />
</Routes>
}
ClassMethod deletePet(pid As %String) As %Status
{
Try {
If '##class(%REST.Impl).%CheckAccepts("application/json") Do ##class(%REST.Impl).%ReportRESTError(..#HTTP406NOTACCEPTABLE,$$$ERROR($$$RESTBadAccepts)) Quit
If ($number(pid,"I")="") Do ##class(%REST.Impl).%ReportRESTError(..#HTTP400BADREQUEST,$$$ERROR($$$RESTInvalid,"id",id)) Quit
Set response=##class(petstore.impl).deletePet(pid)
Do ##class(petstore.impl).%WriteResponse(response)
} Catch (ex) {
Do ##class(%REST.Impl).%ReportRESTError(..#HTTP500INTERNALSERVERERROR,ex.AsStatus())
}
Quit $$$OK
}
Is there a way to generate a YAML documentation for the endpoint /pets/:id, HTTP DELETE method, like you did for REST.Test.Person class?
Sure:
Set sc=##class(%REST.API).GetWebRESTApplication(namespace,webApplication,.swagger)Hmm... interesting... I think I didn't know that class beacuse I still didn't move to IRIS...
Thank you Eduard.
FYI, we're looking to add automatic OpenAPI generation to https://github.com/intersystems/apps-rest at some point in the reasonably-near future. (We had an intern work on it over the summer, and are just kicking the tires on it a bit on our own REST models/APIs before unleashing it on the world.)
Thank you Timothy for let me know.
Don't know if that can help, but with this module, you can do the other way around from swagger generate persitent classes.
https://openexchange.intersystems.com/package/objectscript-openapi-defi…
Hi Guillaume.
One more cool feature to explore when my company moves to IRIS. :)
Thank you for let me know.
💡 This article is considered as InterSystems Data Platform Best Practice.
I am looking to try and generate a swagger spec from a persistent class. The link to the above mentioned code is not working. Could you repost the link or add the code to generate the spec?
Thanks Dan
Have a look at this article/openexchange example :
https://community.intersystems.com/post/how-quickly-publish-restful-api…
it might suit you.
Thanks for the help!
Thanks for noticing! Updated the link.