I am relatively new to swagger but I’m trying to use it to model a new REST API we are creating. I have an API specified in Swagger 2.0. I’ve then used the Swagger Editor to generate a client library in Java and I’m using the Node.js “Swagger Express Middleware” to create a mocking server based on my schema. I have then turned to Java and attempted to send a POST request to the back end. What I’m seeing is that all the attributes that I do not specify in Java are being sent as null. This is causing a parse failure on the JSON validation at the Node.JS server.
For example – Here is some Java code:
public class QueuesTest
{
private static DefaultApi apiClient;
private String C_MSG_VPN = "default";
@BeforeClass
public static void initApi() {
apiClient = new DefaultApi();
apiClient.getApiClient().setBasePath("http://192.168.1.110:8081/v1");
}
@Test
public void testSimpleQueueCRUD() throws ApiException
{
String queueName = "Q/test/queue";
MessageSpoolQueue queueToCreate = new MessageSpoolQueue();
queueToCreate.setName(queueName);
queueToCreate.setMaxSpoolUsage("3000");
queueToCreate.setAccessType(AccessTypeEnum.EXCLUSIVE);
queueToCreate.setConsumerAckPropagation(false);
MessageSpoolQueue createResponse = apiClient.messageVpnsVpnNameMessageSpoolQueuesPost(queueToCreate, C_MSG_VPN);
assertNotNull(createResponse);
assertEquals(queueToCreate, createResponse);
assertEquals(queueName, createResponse.getName());
List<MessageSpoolQueue> response = apiClient.messageVpnsVpnNameMessageSpoolQueuesGet(C_MSG_VPN);
assertNotNull(response);
}
}
My swagger.json definition for this object is:
messageSpoolQueue:
example:
accessType: exclusive
consumerAckPropagation: true
name: exampleQueue
properties:
accessType:
enum:
- exclusive
- non-exclusive
type: string
consumerAckPropagation:
type: boolean
event:
$ref: '#/definitions/messageSpoolQueueEvent'
maxSpoolUsage:
type: string
name:
description: ID of the queue.
type: string
owner:
type: string
maxBindCount:
type: string
...{removed extra attributes for brevity}...
type: object
In the above, I didn’t specify “owner” or “maxBindCount” in the Java unit test. The node.js server reports the following:
swagger:middleware POST /v1/messageVpns/default/messageSpool/queues matches Swagger path /messageVpns/{vpnName}/messageSpool/queues +0ms
swagger:middleware Parsing 2 request parameters... +17ms
swagger:middleware Parsing the "body" body parameter +0ms
Error: The "body" body parameter is invalid ({"accessType":"exclusive","consumerAckPropagation":false,"event":null,"maxBindCount":null,"maxDeliveredUnackedMsgsPerFlow":null,"maxMessageSize":null,"maxRedelivery":null,"maxSpoolUsage":"3000","maxTtl":null,"name":"Q/test/queue","owner":null,"permission":null,"rejectLowPriorityMsg":null,"rejectLowPriorityMsgLimit":null,"rejectMsgToSenderOnDiscard":null,"respectTtl":null,"shutdown":null,"vpnName":null})
Data path: "/maxBindCount"
Schema path: "/properties/maxBindCount/type"
Invalid type: null (expected string)
at ono (/home/demo/sempv2MockServer/node_modules/swagger-express-middleware/node_modules/ono/lib/index.js:62:17)
at parseParameter (/home/demo/sempv2MockServer/node_modules/swagger-express-middleware/lib/param-parser.js:146:11)
at /home/demo/sempv2MockServer/node_modules/swagger-express-middleware/lib/param-parser.js:93:18
at Array.some (native)
at parseBodyParam (/home/demo/sempv2MockServer/node_modules/swagger-express-middleware/lib/param-parser.js:77:10)
at Layer.handle [as handle_request] (/home/demo/sempv2MockServer/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/demo/sempv2MockServer/node_modules/express/lib/router/index.js:312:13)
at /home/demo/sempv2MockServer/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/home/demo/sempv2MockServer/node_modules/express/lib/router/index.js:330:12)
at next (/home/demo/sempv2MockServer/node_modules/express/lib/router/index.js:271:10)
Based on some searching, I believe I’d like to figure out a way to make use of the JsonSerialize markup for non-null. (@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) But I can’t quite figure out is how to do this from Swagger so that the Java client libraries would just auto generate correctly. Is there some markup I’m missing in my Swagger definition? Or am I totally off in the wrong path here?
Any help would be greatly appreciated.
Thanks
Mark
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)--
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/EnEURydhPZA/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.