我正在尝试使用ExampleBuilder.fromProperty从庞大的文件中获取示例
下面是我使用过的库
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-inflector</artifactId>
<version>1.0.1</version>
</dependency>
下面是我得到的输出
{
"name": "InitiatePaymentRequest",
"namespace": null,
"prefix": null,
"attribute": false,
"wrapped": false,
"wrappedName": null,
"typeName": "object",
"values": {
"beneficiaryInformation": {
"name": "BeneficiaryInformation",
"namespace": null,
"prefix": null,
"attribute": false,
"wrapped": false,
"wrappedName": null,
"typeName": "object",
"values": {
"destinationAccountIdentifier": {
"name": "destinationAccountIdentifier",
"namespace": null,
"prefix": null,
"attribute": false,
"wrapped": false,
"wrappedName": null,
"typeName": "string",
"value": "string"
},
"destinationBankIdentifier": {
"name": "destinationBankIdentifier",
"namespace": null,
"prefix": null,
"attribute": false,
"wrapped": false,
"wrappedName": null,
"typeName": "string",
"value": "DEUTDEDB237"
},
"fullName": {
"name": "fullName",
"namespace": null,
"prefix": null,
"attribute": false,
"wrapped": false,
"wrappedName": null,
"typeName": "string",
"value": "JASON SMITH"
}
}
},
"purposeOfPayment": {
"name": "purposeOfPayment",
"namespace": null,
"prefix": null,
"attribute": false,
"wrapped": false,
"wrappedName": null,
"typeName": "string",
"value": "Invoice Payment"
},
"remittanceInformation": {
"name": "remittanceInformation",
"namespace": null,
"prefix": null,
"attribute": false,
"wrapped": false,
"wrappedName": null,
"typeName": "string",
"value": "Mantainance of Fixtures"
},
"remitterInformation": {
"name": "RemitterInformation",
"namespace": null,
"prefix": null,
"attribute": false,
"wrapped": false,
"wrappedName": null,
"typeName": "object",
"values": {
"sourceAccountCurrency": {
"name": "sourceAccountCurrency",
"namespace": null,
"prefix": null,
"attribute": false,
"wrapped": false,
"wrappedName": null,
"typeName": "string",
"value": "EUR"
},
"sourceAccountIdentifier": {
"name": "sourceAccountIdentifier",
"namespace": null,
"prefix": null,
"attribute": false,
"wrapped": false,
"wrappedName": null,
"typeName": "string",
"value": "string"
},
"sourceBankIdentifier": {
"name": "sourceBankIdentifier",
"namespace": null,
"prefix": null,
"attribute": false,
"wrapped": false,
"wrappedName": null,
"typeName": "string",
"value": "DEUTDEDBFRA"
}
}
},
"transferAmount": {
"name": "transferAmount",
"namespace": null,
"prefix": null,
"attribute": false,
"wrapped": false,
"wrappedName": null,
"typeName": "decimal",
"value": 1.5
},
"transferCurrency": {
"name": "transferCurrency",
"namespace": null,
"prefix": null,
"attribute": false,
"wrapped": false,
"wrappedName": null,
"typeName": "string",
"value": "EUR"
},
"transferDate": {
"name": "transferDate",
"namespace": null,
"prefix": null,
"attribute": false,
"wrapped": false,
"wrappedName": null,
"typeName": "string",
"value": "2015-07-20"
},
"transferType": {
"name": "transferType",
"namespace": null,
"prefix": null,
"attribute": false,
"wrapped": false,
"wrappedName": null,
"typeName": "string",
"value": "SCTInst"
},
"uniqueRequestNo": {
"name": "uniqueRequestNo",
"namespace": null,
"prefix": null,
"attribute": false,
"wrapped": false,
"wrappedName": null,
"typeName": "string",
"value": "string"
}
}
}
以下是我的预期输出:
{
"beneficiaryInformation": {
"destinationAccountIdentifier": "string",
"destinationBankIdentifier": "DEUTDEDB237",
"fullName": "JASON SMITH"
},
"purposeOfPayment": "Invoice Payment",
"remittanceInformation": "Mantainance of Fixtures",
"remitterInformation": {
"sourceAccountCurrency": "EUR",
"sourceAccountIdentifier": "string",
"sourceBankIdentifier": "DEUTDEDBFRA"
},
"transferAmount": 1.5,
"transferCurrency": "EUR",
"transferDate": "2015-07-20",
"transferType": "SCTInst",
"uniqueRequestNo": "string"
}
需要一些有关如何实现相同目标的建议。谢谢
我正在尝试使用ExampleBuilder.fromProperty从庞大的文件中获取示例
这是我的模型定义
"InitiatePaymentRequest": {
"type": "object",
"properties": {
"beneficiaryInformation": {
"description": "the details of the beneficiary",
"$ref": "#/definitions/BeneficiaryInformation"
},
"purposeOfPayment": {
"type": "string",
"example": "Invoice Payment",
"description": "the purpose for which the payment is being made",
"minLength": 0,
"maxLength": 35,
"pattern": "[A-Z0-9a-z?:().,'+/\\- ]*"
},
"remittanceInformation": {
"type": "string",
"example": "Mantainance of Fixtures",
"description": "a free text that may be received by the beneficiary",
"minLength": 0,
"maxLength": 140,
"pattern": "[A-Z0-9a-z?:().,'+/\\- ]*"
},
"remitterInformation": {
"description": "the details of the remitter",
"$ref": "#/definitions/RemitterInformation"
},
"transferAmount": {
"type": "number",
"example": 123.01,
"description": "the amount to transfer",
"minimum": 0,
"exclusiveMinimum": true
},
"transferCurrency": {
"type": "string",
"example": "EUR",
"description": "the currency for the transfer amount",
"enum": [
"EUR"
]
},
"transferDate": {
"type": "string",
"format": "date",
"description": "the date for the transfer, should always be the current business date"
},
"transferType": {
"type": "string",
"example": "SCTInst",
"description": "the payment network to be used for the transfer",
"enum": [
"SCTInst"
]
},
"uniqueRequestNo": {
"type": "string",
"description": "a unique value that is used to identify each distinct request",
"minLength": 1,
"maxLength": 35,
"pattern": "[A-Z0-9a-z]*"
}
},
"title": "InitiatePaymentRequest"
}
下面是我的Java代码:
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
Swagger swagger = new SwaggerParser()
.read("swagger.json");
swagger.getPaths().size();
swagger.getPaths().forEach((k, v) -> {
GenPactRequest req = new GenPactRequest();
req.setName(swagger.getInfo().getTitle());
v.getOperationMap().forEach((a, b) -> {
SimpleModule simpleModule = new SimpleModule().addSerializer(new JsonNodeExampleSerializer());
Json.mapper().registerModule(simpleModule);
List<Parameter> body = b.getParameters();
ApiRequest apiReq = new ApiRequest();
req.setDescription(b.getSummary());
req.setProvider(b.getSummary());
req.setApiCode("sdf23e3r");
apiReq.setPath(k);
apiReq.setMethod(a.toString());
// operation is GET, with no input
if (body.size() == 0) {
req.setRequest(writeValueAsString(mapper, apiReq));
b.getResponses().forEach((x, y) -> {
if (x == "200") {
req.setResponse(writeValueAsString(mapper,
responseBody(req, x, b, mapper, swagger, y.getSchema())));
}
});
log.info(writeValueAsString(mapper, req));
return;
}
// api with GET/POST, with path-param/body
for (Parameter parameter : body) {
// api with POST, with body
if (parameter instanceof BodyParameter) {
BodyParameter bp = (BodyParameter) parameter;
apiReq.setBody(Json.pretty(ExampleBuilder.fromProperty(
new RefProperty(bp.getSchema().getReference()), swagger.getDefinitions())));
req.setRequest(writeValueAsString(mapper, apiReq));
b.getResponses().forEach((x, y) -> {
if (x == "200") {
req.setResponse(writeValueAsString(mapper,
responseBody(req, x, b, mapper, swagger, y.getSchema())));
}
});
log.info(writeValueAsString(mapper, req));
return;
}
// api with GET, with path param
else if (parameter instanceof PathParameter) {
req.setRequest(writeValueAsString(mapper, apiReq));
}
b.getResponses().forEach((x, y) -> {
if (x == "200") {
req.setResponse(writeValueAsString(mapper,
responseBody(req, x, b, mapper, swagger, y.getSchema())));
}
});
log.info(writeValueAsString(mapper, req));
return;
}
});
});
}
private static ApiResponse responseBody(GenPactRequest req, String status, Operation op, ObjectMapper mapper,
Swagger swagger, Property property) {
ApiResponse apiRes = new ApiResponse();
apiRes.setStatus(Integer.valueOf(status));
ApiResponseHeader header = new ApiResponseHeader();
header.setHeaders(op.getProduces().get(0));
apiRes.setHeaders(header);
apiRes.setBody(Json.pretty(ExampleBuilder.fromProperty(property, swagger.getDefinitions())));
return apiRes;
}
private static String writeValueAsString(ObjectMapper mapper, Object obj) {
try {
return mapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
log.info("Unable to convert into json");
e.printStackTrace();
}
return null;
}
答案 0 :(得分:0)
找到解决方法:
JSONObject obj =新的JSONObject(Json.pretty(ExampleBuilder.fromProperty( new RefProperty(bp.getSchema()。getReference()),swagger.getDefinitions())))
apiReq.setBody(obj.toMap());
这给了我预期的输出。