Anonymous
Могу ли я создать DslPart, используя существующую часть новой?
Сообщение
Anonymous » 06 авг 2024, 10:12
Я пишу несколько Pact-тестов конечной точки, которая возвращает конкретный продукт, когда идентификатор передается как параметр пути, или массив всех продуктов, когда идентификатор не указан:
< em>{baseUrl}/products/1
Код: Выделить всё
{
"id": 1,
"name": "product1",
"type": "example",
"attributes": [
{
"attrib1": "text1",
"attrib2": "text2",
"attrib3": "text3",
},
{
"attrib1": "string1",
"attrib2": "string2",
"attrib3": "string2",
},
]
}
{baseUrl}/products
Код: Выделить всё
{
"products": [
{
"id": 1,
"name": "product1",
"type": "example",
"attributes": [
{
"attrib1": "text1",
"attrib2": "text2",
"attrib3": "text3"
},
{
"attrib1": "string1",
"attrib2": "string2",
"attrib3": "string3"
}
]
},
{
"id": 2,
"name": "product2",
"type": "example",
"attributes": [
{
"attrib1": "text1",
"attrib2": "text2",
"attrib3": "text3"
},
{
"attrib1": "string1",
"attrib2": "string2",
"attrib3": "string3"
},
{
"attrib1": "abc1",
"attrib2": "abc2",
"attrib3": "abc3"
}
]
},
{
"id": 3,
"name": "product3",
"type": "example",
"attributes": [
{
"attrib1": "text1",
"attrib2": "text2",
"attrib3": "text3"
}
]
}
]
}
Я создаю тело JSON отдельного продукта с помощью Lambda DSL:
Код: Выделить всё
DslPart product = newJsonBody((body) -> {
body.numberType("id", 1);
body.stringType("name", "product1");
body.stringType("type", "example");
body.array("attributes", (attributes) -> {
attributes.object((attrib) -> {
attrib.stringType("attrib1", "text1");
attrib.stringType("attrib2", "text2");
attrib.stringType("attrib3", "text3");
});
});
}).build();
Пока все хорошо, но теперь я хочу создать тело ответа из списка продуктов и повторно использовать продукт DslPart, который у меня уже есть.
Примерно так:
Код: Выделить всё
DslPart productList = newJsonBody((body) -> {
body.array("products", (products) -> {
products.object(product); // This is not possible, a LambdaDslObject is required
});
}).build();
До сих пор я нашел единственный способ — переписать весь продукт внутри массива продуктов
Подробнее здесь:
https://stackoverflow.com/questions/787 ... he-new-one
1722928356
Anonymous
Я пишу несколько Pact-тестов конечной точки, которая возвращает конкретный продукт, когда идентификатор передается как параметр пути, или массив всех продуктов, когда идентификатор не указан: < em>{baseUrl}/products/1 [code]{ "id": 1, "name": "product1", "type": "example", "attributes": [ { "attrib1": "text1", "attrib2": "text2", "attrib3": "text3", }, { "attrib1": "string1", "attrib2": "string2", "attrib3": "string2", }, ] } [/code] {baseUrl}/products [code]{ "products": [ { "id": 1, "name": "product1", "type": "example", "attributes": [ { "attrib1": "text1", "attrib2": "text2", "attrib3": "text3" }, { "attrib1": "string1", "attrib2": "string2", "attrib3": "string3" } ] }, { "id": 2, "name": "product2", "type": "example", "attributes": [ { "attrib1": "text1", "attrib2": "text2", "attrib3": "text3" }, { "attrib1": "string1", "attrib2": "string2", "attrib3": "string3" }, { "attrib1": "abc1", "attrib2": "abc2", "attrib3": "abc3" } ] }, { "id": 3, "name": "product3", "type": "example", "attributes": [ { "attrib1": "text1", "attrib2": "text2", "attrib3": "text3" } ] } ] } [/code] Я создаю тело JSON отдельного продукта с помощью Lambda DSL: [code]DslPart product = newJsonBody((body) -> { body.numberType("id", 1); body.stringType("name", "product1"); body.stringType("type", "example"); body.array("attributes", (attributes) -> { attributes.object((attrib) -> { attrib.stringType("attrib1", "text1"); attrib.stringType("attrib2", "text2"); attrib.stringType("attrib3", "text3"); }); }); }).build(); [/code] Пока все хорошо, но теперь я хочу создать тело ответа из списка продуктов и повторно использовать продукт DslPart, который у меня уже есть. Примерно так: [code]DslPart productList = newJsonBody((body) -> { body.array("products", (products) -> { products.object(product); // This is not possible, a LambdaDslObject is required }); }).build(); [/code] До сих пор я нашел единственный способ — переписать весь продукт внутри массива продуктов Подробнее здесь: [url]https://stackoverflow.com/questions/78738110/can-i-build-a-dslpart-using-an-existing-one-as-part-of-the-new-one[/url]