I have a GET endpoint in my application supposed to return :
{
"gameId": "41a483c4-6220-424a-a931-d9114a4f6748",
"pits": [
{
"id": 1,
"stones": 6
},
{
"id": 2,
"stones": 6
},
{
"id": 3,
"stones": 6
},
{
"id": 4,
"stones": 6
},
{
"id": 5,
"stones": 6
},
{
"id": 6,
"stones": 6
},
{
"id": 7,
"stones": 0
},
{
"id": 8,
"stones": 6
},
{
"id": 9,
"stones": 6
},
{
"id": 10,
"stones": 6
},
{
"id": 11,
"stones": 6
},
{
"id": 12,
"stones": 6
},
{
"id": 13,
"stones": 6
},
{
"id": 14,
"stones": 0
}
],
"playerTurn": null,
"currentPitIndex": 0
}
but instead it returns:
{
"id": "25f09303-b797-418f-a7e7-db0e5fa8631b",
"pits": [
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 0,
"empty": true
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 0,
"empty": true
}
],
"playerTurn": null,
"currentPitIndex": null
}
I am wondering what is "empty"?! and where is "id"!
would be much appreciated for any suggestion and help.
thank you
Does your class have a method called isEmpty()? Because most JSON marshalling frameworks add JSON properties for any method with 0 arguments that starts with get, or returns a boolean and starts with is. For the same reason id is probably missing - I'm guessing there is no getter for it. The top level gameId is probably called id because you have a getId() method, not a getGameId() method.
For most frameworks you can tweak this with annotations. For instance, with Jackon you can use #JsonIgnore to indicate a method should not be represented as a JSON property, and #JsonProperty can be used to a) add a custom name, or b) allow a field to be included as well. For other frameworks you should check their documentation.
Related
I'm trying to validate the response schema with karate but facing issue with array.
Attaching the response and feature as well as my schema.json.
Response -
{
"page": 1,
"per_page": 6,
"total": 12,
"total_pages": 2,
"data": [
{
"id": 3,
"email": "emma.wong#reqres.in",
"first_name": "Emma",
"last_name": "Wong",
"avatar": "https://reqres.in/img/faces/3-image.jpg"
},
{
"id": 4,
"email": "eve.holt#reqres.in",
"first_name": "Eve",
"last_name": "Holt",
"avatar": "https://reqres.in/img/faces/4-image.jpg"
},
{
"id": 5,
"email": "charles.morris#reqres.in",
"first_name": "Charles",
"last_name": "Morris",
"avatar": "https://reqres.in/img/faces/5-image.jpg"
},
{
"id": 6,
"email": "tracey.ramos#reqres.in",
"first_name": "Tracey",
"last_name": "Ramos",
"avatar": "https://reqres.in/img/faces/6-image.jpg"
}
],
"support": {
"url": "https://reqres.in/#support-heading",
"text": "To keep ReqRes free, contributions towards server costs are appreciated!"
}
}
Scenario: Get all Users and validate schema
Given url getUrl
When method Get
Then status 200
And print response
Then match response == '#object'
* string jsonSchemaExpected = read('file:src/test/resources/features/sample/responseSchema.json')
And print response.data.length
And match response == jsonSchemaExpected
responseSchema.json
{
"page": "#number",
"per_page": "#number",
"total": "#number",
"total_pages": "#number",
"data": "#[] #object",
"support": "#object"
}
The only observation I have is if you cast to a string, you won't be able to do any matching.
Instead of * string jsonSchemaExpected do * def jsonSchemaExpected.
I'm trying to filter a List of objects that have the same property in JAVA. For example, I have this json
{
"id": 0,
"brand": "seat",
"brand_id": 1,
"model": "ibiza",
"year": 2010
},
{
"id": 1,
"brand": "seat",
"brand_id": 1,
"model": "leon",
"year": 2015
},
{
"id": 2,
"brand": "seat",
"brand_id": 1,
"model": "alhambra",
"year": 2008
},
{
"id": 3,
"brand": "ford",
"brand_id": 2,
"model": "focus",
"year": 2005
},
{
"id": 4,
"brand": "ford",
"brand_id": 2,
"model": "mondeo",
"year": 2018
},
{
"id": 5,
"brand": "ford",
"brand_id": 2,
"model": "fiesta",
"year": 2015
}
Now, if I'm receiving for example the "brand_id" parameter through a get petition, I need something like this. If I receive brand_id = 2 it shows:
{
"id": 3,
"brand": "ford",
"brand_id": 2,
"model": "focus",
"year": 2005
},
{
"id": 4,
"brand": "ford",
"brand_id": 2,
"model": "mondeo",
"year": 2018
},
{
"id": 5,
"brand": "ford",
"brand_id": 2,
"model": "fiesta",
"year": 2015
}
How can I achieve that? Thanks in advance
data.findAll { it.brand == "ford" }
I think this will serve your purpose.
import java.util.stream.Collectors
import groovy.json.JsonSlurper
def json = '[{"id": 0, "brand": "seat", "brand_id": 1, "model": "ibiza", "year": 2010 }, { "id": 1, "brand": "seat", "brand_id": 1, "model": "leon", "year": 2015 }, { "id": 2, "brand": "seat", "brand_id": 1, "model": "alhambra", "year": 2008 }, { "id": 3, "brand": "ford", "brand_id": 2, "model": "focus", "year": 2005 }, { "id": 4, "brand": "ford", "brand_id": 2, "model": "mondeo", "year": 2018 }, { "id": 5, "brand": "ford", "brand_id": 2, "model": "fiesta", "year": 2015 }]'
def jsonSlurper = new JsonSlurper()
def jsonObj = jsonSlurper.parseText(json)
def carsWithId2 = ((List)jsonObj).findAll{car -> car.brand_id == 2}
print carsWithId2
This prints:
[[brand:ford, brand_id:2, id:3, model:focus, year:2005], [brand:ford, brand_id:2, id:4, model:mondeo, year:2018], [brand:ford, brand_id:2, id:5, model:fiesta, year:2015]]
Got the following JSON:
{
"content": [
7,
8,
9,
10
],
"last": true,
"total_elements": 9,
"total_pages": 2,
"first": false,
"number_of_elements": 4,
"size": 5,
"number": 1,
"empty": false
}
and I would like to create a contract with PACT using the following code:
DslPart body = newJsonBody((root) -> {
root.array("consumer", a -> a.integerType().integerType().integerType())
.booleanType("last")
.numberType("total_elements")
.numberType("total_pages")
.booleanType("first")
.numberType("number_of_elements")
.numberType("size")
.numberType("number")
.booleanType("empty");
}).build();
Although this is working, I really do not like the array contract using a concatenation of integerType.
My question is: is there a better way to indicate an array of X elements, which have to be of type Integer?
You can use minArrayLike.
#Test
public void test() {
DslPart body = newJsonBody((root) ->
root.minArrayLike("content", 4, PactDslJsonRootValue.integerType(1), 4)
.booleanType("last")
.numberType("total_elements")
.numberType("total_pages")
.booleanType("first")
.numberType("number_of_elements")
.numberType("size")
.numberType("number")
.booleanType("empty"))
.build();
System.out.println(body.toString());
}
Produces
{
"content": [
1,
1,
1,
1
],
"number": 100,
"last": true,
"size": 100,
"total_elements": 100,
"total_pages": 100,
"number_of_elements": 100,
"first": true,
"empty": true
}
if the size is larger than the number of records, my object Page returns empty.
parameters: size=100&page=1
{
"content": [],
"pageable": {
"sort": {
"sorted": false,
"unsorted": true,
"empty": true
},
"offset": 100,
"pageSize": 100,
"pageNumber": 1,
"paged": true,
"unpaged": false
},
"totalElements": 69,
"totalPages": 1,
"last": true,
"size": 100,
"number": 1,
"numberOfElements": 0,
"first": false,
"sort": {
"sorted": false,
"unsorted": true,
"empty": true
},
"empty": true
}
How to return only the records found?
tks!
As I know, in the Spring Data, by default, the page number is started from 0.
And you try to get page=1 (this is the second page).
I have a simple Spring Boot application that has the next method inside my controller:
#RequestMapping(method = RequestMethod.GET, value = "/")
public ResponseEntity<Page> getUsers(Pageable pageable) {
Page<User> users = repository.findAll(pageable);
return ResponseEntity.ok(users);
}
My records are being fetched using simple pageable repository. The response generated is:
{
"content": [
{
...
},
{
...
}
],
"pageable": {
"sort": {
"sorted": false,
"unsorted": true
},
"offset": 0,
"pageNumber": 0,
"pageSize": 20,
"paged": true,
"unpaged": false
},
"totalPages": 1,
"totalElements": 3,
"last": true,
"size": 20,
"number": 0,
"sort": {
"sorted": false,
"unsorted": true
},
"numberOfElements": 3,
"first": true
}
How do I achieve same result as when I use ResourceSupport, which is:
.....
"page": {
"size": 20,
"totalElements": 3,
"totalPages": 1,
"number": 0
}
Reason is - I do not need the links generation and so do not want to deal with the ResourceSupport/Assembler facilities of hateoas.
Is there any built-in option (besides writing my own serialization for Page < T >) ?