Examplebuilder usage in Swagger documents

139 views
Skip to first unread message

Shreejit Nair

unread,
Jun 27, 2016, 8:40:34 AM6/27/16
to Swagger
Hi,
I am wondering if anyone has luck trying to use ExampleBuilder to push in default data that comes along with the swagger specification document into one of the operations. I am automatically trying to post to an endpoint with the sample data that is defined and which we can see from the swagger UI.
I already have most of the object model extracted from the spec but unable to find a way how to execute a certain operation with the data provided in the swagger programmatically rather than from the UI.
Any working example would be really helpful.

tony tam

unread,
Jun 27, 2016, 9:54:47 AM6/27/16
to swagger-sw...@googlegroups.com
Please take a look at the tests in the inflector project, as well as how it’s used in code.  It should be straightforward to follow:


--
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.

Shreejit Nair

unread,
Jun 27, 2016, 10:53:52 AM6/27/16
to Swagger
Thanks Tony,

// This is exactly what i need

assertEqualsIgnoreLineEnding(Json.pretty(ExampleBuilder.fromProperty(new RefProperty("User"), definitions))


Could you tell me how i could programmatically extract the RefProperty from the swagger specification?

such that i get the #definition/User and then use it to feed it to the below function?


This is a bit of code below that loops through all Parameters for an endpoint.

There should be a way to extract the $ref here and resolve it to "User"?.

I am unable to figure out how to in the object model.

None of the models map to instance of RefModel during execution for the pet example


for (Parameter param : parameterList) {

System.out.println(param.getClass());

if (param instanceof BodyParameter) {

//System.out.println("BODY PARAMETER WAS FOUND for a put operation");

BodyParameter bp = (BodyParameter) param;

Model model = bp.getSchema();

if (model instanceof RefModel) {

RefModel ref = (RefModel) model;  // Never gets executed

String simpleRef = ref.getSimpleRef();

System.out.println(simpleRef);

Model concreteModel = swagger.getDefinitions().get(simpleRef);

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

tony tam

unread,
Jun 27, 2016, 11:49:49 AM6/27/16
to swagger-sw...@googlegroups.com
Hi, I think you’ve asked this same question a few times.  The RefModel will be returned by the parser automatically, unless you use “resolveFully” in the parse logic, which you will never get a ref model 

On Jun 27, 2016, at 7:53 AM, Shreejit Nair <shreeji...@gmail.com> wrote:

Thanks Tony,

// This is exactly what i need

assertEqualsIgnoreLineEnding(Json.pretty(ExampleBuilder.fromProperty(newRefProperty("User"), definitions))

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

Shreejit Nair

unread,
Jun 27, 2016, 11:56:13 AM6/27/16
to swagger-sw...@googlegroups.com
Yes Tony,
I am using ResolveFully() already and my model is always of type ModelImpl.
What should my code look like to extract the $ref now.
I apologize for asking this in the past but since i am doing everything programmatically via the API it is a bit difficult to understand the object model.
I only had a chance now to work on this.

Should i remove the resolveFully() and try getting the Object example using RefModel.
Will that resolve the internal schemas as well?
OR
Extract the $ref from ModelImp.

I promise not to bother you again. This is the only bit pending for me now in terms of using the API.

assertEqualsIgnoreLineEnding(Json.pretty(ExampleBuilder.fromProperty(newRefProperty("User"), definitions))

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.

-- 
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.

--
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/R4dcZ2nvXCE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to swagger-swaggers...@googlegroups.com.

tony tam

unread,
Jun 27, 2016, 12:19:15 PM6/27/16
to swagger-sw...@googlegroups.com
ResolveFully will remove _all_ refs and put complete objects in place.  So if you remove that call, it should have the $ref values

I don’t mind you asking questions, of course :) 

Shreejit Nair

unread,
Jun 27, 2016, 12:33:57 PM6/27/16
to swagger-sw...@googlegroups.com
Exactly so when i do the resolveFully()
How do i extract the object out especially the sample data that is already populated in the swagger document.
Also, sometimes it is an ArrayModel and not a RefModel instance.
How do we auto populate data in the case?
Or does the petswagger have an example as well then?

Essentially and eventually:
1. for put -> post get the sample data from the swagger document

If i do a resolveFully() how do i eventually extract the ExampleBuilder sample data that swagger gives us.
Is there a way to pass this ModelImpl instance to ExampleBuilder() to extract the data out of it?
Then, i understand since $ref is already resolved to a full object it is  just a question of extracting the sample data out of it.

Shreejit Nair

unread,
Jun 27, 2016, 4:48:50 PM6/27/16
to Swagger
Hi Tony,
I managed to do this way without the ResolveFully().
Please let me know if the model can be RefModel or ArrayModel only when we try to dereference $ref to feed the actual values.

if (model instanceof RefModel) {

System.out.println("Model was found of type RefModel");

RefModel ref = (RefModel) model;

String simpleRef = ref.getSimpleRef();

System.out.println(simpleRef);

Model concreteModel = swagger.getDefinitions().get(simpleRef);

Object test = ExampleBuilder.fromProperty(

new io.swagger.models.properties.RefProperty(simpleRef), definitionMap);

if (test != null) {

String str = new XmlExampleSerializer().serialize((Example) test);

System.out.println(str);

}


} else if (model instanceof ArrayModel) {

ArrayModel arrayModel = (ArrayModel) model;

Property prop = arrayModel.getItems();

if (prop instanceof RefProperty) {

Object test = ExampleBuilder.fromProperty(prop, definitionMap);


if (test != null) {

String str = new XmlExampleSerializer().serialize((Example) test);

System.out.println(str);

tony tam

unread,
Jun 27, 2016, 4:57:50 PM6/27/16
to swagger-sw...@googlegroups.com
Hi Shreejit, anywhere you have the interfaces, you may get a compatible concrete class.  Consider testing different definitions on your side to see what the concrete types are in the Swagger.java class.

Shreejit Nair

unread,
Jun 28, 2016, 10:52:35 AM6/28/16
to swagger-sw...@googlegroups.com
Thank you so much Tony,

I am able able to extract the sample json / sample xml / sample array all out now to push it automatically. So everything on the swagger UI i am able to retrieve it from the backend.
Thanks for all the help and guidance.

Shreejit

--
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/R4dcZ2nvXCE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to swagger-swaggers...@googlegroups.com.

tony tam

unread,
Jun 28, 2016, 10:53:06 AM6/28/16
to swagger-sw...@googlegroups.com
Great, thank you for following up on it

subhasis...@gmail.com

unread,
Jun 29, 2016, 5:46:33 AM6/29/16
to Swagger
Hi All, 

It seems very interesting thread.
Can anybody let me know what is this "resolveFully" ?

Thanks
Subhasish

subhasis...@gmail.com

unread,
Jun 29, 2016, 10:29:08 AM6/29/16
to Swagger
Hi All,

What if the model  is not instanceof RefModel or model is not instanceof ArrayModel.

how can we get the example for other Model types.

Thanks
Subhasish
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.

--
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/R4dcZ2nvXCE/unsubscribe.
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.

tony tam

unread,
Jun 29, 2016, 12:22:59 PM6/29/16
to swagger-sw...@googlegroups.com
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

Shreejit Nair

unread,
Jun 29, 2016, 1:34:41 PM6/29/16
to swagger-sw...@googlegroups.com
I think it should always be a ref model or an Array model.

I do not  use the ResolveFully()

This is what I am doing:


if (model instanceof RefModel) {
                            System.out.println("Model was found of type RefModel");
                            RefModel ref = (RefModel) model;
                            String simpleRef = ref.getSimpleRef();
                            System.out.println(simpleRef);
                            Model concreteModel = swagger.getDefinitions().get(simpleRef);
                            Object test = ExampleBuilder.fromProperty(
                                    new io.swagger.models.properties.RefProperty(simpleRef), definitionMap);

                            if (test != null && consumesList != null) {
                                if (consumesList.contains("application/json")) { // similar block if type is xml and so on
                                    jsonString = Json.pretty(test);

                                  // build a data structure out of it

What I am essentially doing is build some data structure that converts the swagger into an object model that can be pushed to a test automation framework.
Then i use RestAssured to automatically validate it for each endpoint on the spec and do some basic sanity tests on them:


case "post":

                if (dataMap.get("validdata") != null) { // check to see if we managed to pull out the example data feed it to a client automatically

                    // Valid data
                    String schema = (String) ((Map) dataMap.get("validdata")).get("schema");
                    String body = (String) ((Map) dataMap.get("validdata")).get("data");
                    String url = dataMap.get("baseURI") + endpointOutput;

                    Response rep = RestAssured.given().contentType(schema).when().body(body).post(url);
                    softAssert.assertEquals(rep.getStatusCode(), HttpStatus.SC_OK);
      
              else
                     Assert.fail("Example data is missing or no Consumes section for endpoint" + endpointOutput
                            + " and operation " + endpointOperationOutput.toLowerCase());

So everything is being extracted from the swagger.
We are essentially trying to 1. run some basic tests programmatically   2. find any gaps in the swagger spec which would be a documentation failure, that is we simply cannot test without this data and we dont want to work around it, rather update the spec and try again

We are trying to build an automation framework that uses only the swagger path and nothing else and see how far we can take it.
Input to framework: "http://petstore.swagger.io/v2/swagger.json"

All the assertion points come from within the spec itself.

Shreejit

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.

--
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/R4dcZ2nvXCE/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 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.

--
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/R4dcZ2nvXCE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to swagger-swaggers...@googlegroups.com.

Shreejit Nair

unread,
Jun 29, 2016, 5:03:34 PM6/29/16
to Swagger
Hi Tony,

Is it possible for the swagger specification to be extended to also document properties and example values for path parameters on an endpoint like say:

GET

/store/order/{orderId}

Can this have some associated $ref such that I can know what value to replace the orderId with at runtime to execute against my test harness?

I do not see a way how I can replace the {orderId} on the fly dynamically without understanding the underlying schema of how things are presented?

Also, I noticed a lot of the post/put operations for the pet swagger did not have a "consumes" section documented though they had example data so I was unable to set the content Type at runtime to execute the endpoint.


ladawn 2mayhall

unread,
Jul 1, 2016, 6:57:52 PM7/1/16
to swagger-sw...@googlegroups.com
Excuse me but what did you email me about 

vijay kumar

unread,
Mar 22, 2017, 3:40:03 AM3/22/17
to Swagger
Hi Shreejit,

I have followed this thread and the code works fine but when i generated object i get teh value as below.Can you tell me how to create the datastructure from it.

{
  "name" : "createFoldersRequest",
  "attribute" : false,
  "wrapped" : false,
  "typeName" : "object",
  "values" : {
    "folders" : {
      "name" : "createFoldersRequestArray",
      "attribute" : false,
      "wrapped" : false,
      "typeName" : "array",
      "items" : [ {
        "name" : "createFoldersRequestArrayContent",
        "attribute" : false,
        "wrapped" : false,
        "typeName" : "object",
        "values" : {
          "folder" : {
            "attribute" : false,
            "wrapped" : false,
            "typeName" : "object",
            "values" : {
              "name" : {
                "attribute" : false,
                "wrapped" : false,
                "typeName" : "string",
                "value" : "string"
              },
              "indexingDisabled" : {
                "attribute" : false,
                "wrapped" : false,
                "typeName" : "boolean",
                "value" : true
              },
              "note" : {
                "attribute" : false,
                "wrapped" : false,
                "typeName" : "string",
                "value" : "string"
              },
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.

--
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/R4dcZ2nvXCE/unsubscribe.
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.

--
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/R4dcZ2nvXCE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to swagger-swaggersocket+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages