Getting schema value from parameters object using swagger-parser

251 views
Skip to first unread message

subhasis...@gmail.com

unread,
May 24, 2016, 8:30:41 AM5/24/16
to Swagger
Hi,

I am trying to parse swagger.json file using swagger-parser , want to create request file and response file in the from of json out of it.

How to get the values of schema from parameter object.

Bellow is the part of the swagger.json file. schema is highlighted.
Also I have a code snippet to read the values from it where "op" is operation object. I did not find any method to read schema like other parameter.

Swagger.json

"/pet": {
"post": {
"tags": ["pet"],
"summary": "Add a new pet to the store",
"description": "",
"operationId": "addPet",
"consumes": ["application/json",
"application/xml"],
"produces": ["application/xml",
"application/json"],
"parameters": [{
"in": "body",
"name": "body",
"description": "Pet object that needs to be added to the store",
"required": true,
"schema": {
"$ref": "#/definitions/Pet"
}
}],
"responses": {
"405": {
"description": "Invalid input"
}
},
"security": [{
"petstore_auth": ["write:pets",
"read:pets"]
}]
},
"put": {
"tags": ["pet"],
"summary": "Update an existing pet",
"description": "",
"operationId": "updatePet",
"consumes": ["application/json",
"application/xml"],
"produces": ["application/xml",
"application/json"],
"parameters": [{
"in": "body",
"name": "body",
"description": "Pet object that needs to be added to the store",
"required": true,
"schema": {
"$ref": "#/definitions/Pet"
}
}],
"responses": {
"400": {
"description": "Invalid ID supplied"
},
"404": {
"description": "Pet not found"
},
"405": {
"description": "Validation exception"
}
},
"security": [{
"petstore_auth": ["write:pets",
"read:pets"]
}]
}
}

Code Snippet

 try{
 
List<Parameter> parameters=op.getParameters();
for(Parameter p:parameters)
{
System.out.println("p.getIn()"+p.getIn());
System.out.println("p.getName()"+p.getName());
System.out.println("p.getAccess()"+p.getAccess());
System.out.println("p.getDescription()"+p.getDescription());
System.out.println("p.getPattern()"+p.getPattern());
System.out.println("p.getRequired()"+p.getRequired());
}
System.out.println("\n");
}
catch(NullPointerException n)
{
 System.out.println("no value found");
}


tony tam

unread,
May 24, 2016, 12:46:44 PM5/24/16
to swagger-sw...@googlegroups.com
if (p instanceof BodyParameter) {
  BodyParameter bodyParameter = (BodyParameter) p;
  Model model = bodyParameter.getSchema();
  // ... do your thing
}

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

subhasis...@gmail.com

unread,
May 25, 2016, 6:21:32 AM5/25/16
to Swagger
Hi Tony,

Now I understand it depends upon the Paramter type I found the following types of parameter.













But what is the use of RefParamter and CookieParameter.
I did not found any details about them in specification.

Thanks
Subhasish 

Reply all
Reply to author
Forward
0 new messages