Skip to content

Commit c86f7b6

Browse files
committed
Update version to 1.9.0
Add trace object with id field to meta object Update doc
1 parent a00f281 commit c86f7b6

File tree

6 files changed

+159
-9
lines changed

6 files changed

+159
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [1.9.0] - 2022-11-07
4+
### Added:
5+
- Trace object with id field to meta object
6+
37
## [1.8.0] - 2022-11-01
48
### Added:
59
- Error links object support

README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Add dependency to your project:
1616
<dependency>
1717
<groupId>com.slm-dev</groupId>
1818
<artifactId>jsonapi-simple</artifactId>
19-
<version>1.8.0</version>
19+
<version>1.9.0</version>
2020
</dependency>
2121
```
2222

@@ -578,3 +578,48 @@ public class Test {
578578
}
579579
}
580580
```
581+
582+
Example response with trace id (trace id can be string or UUID):
583+
```java
584+
// Pseudo code
585+
public class Test {
586+
public Response<Void> main() {
587+
return Response.<Void, Void>builder()
588+
.data(
589+
TestDto.builder()
590+
.id(UUID.randomUUID())
591+
.name("Test string")
592+
.createDate(LocalDateTime.now(ZoneOffset.UTC))
593+
.build()
594+
).metaTrace(new Meta.Trace(UUID.randomUUID()))
595+
.build();
596+
}
597+
}
598+
```
599+
```json
600+
{
601+
"data": {
602+
"type":"test-object",
603+
"id":"7a543e90-2961-480e-b1c4-51249bf0c566",
604+
"attributes": {
605+
"name":"Test string",
606+
"createDate":"2019-10-08T18:46:53.40297"
607+
},
608+
"links": {
609+
"self": "/test-object/7a543e90-2961-480e-b1c4-51249bf0c566"
610+
}
611+
},
612+
"meta": {
613+
"api": {
614+
"version":"1"
615+
},
616+
"page": {
617+
"maxSize":25,
618+
"total":1
619+
},
620+
"trace": {
621+
"id": "1a443e90-6961-480e-b1c4-51249bf0c566"
622+
}
623+
}
624+
}
625+
```

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.slm-dev</groupId>
66
<artifactId>jsonapi-simple</artifactId>
7-
<version>1.8.0</version>
7+
<version>1.9.0</version>
88
<name>jsonapi-simple</name>
99
<description>Simple implementation of the JSON:API specification</description>
1010
<url>https://slm-dev.com/jsonapi-simple/</url>

src/main/java/com/slmdev/jsonapi/simple/response/Meta.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ public class Meta {
1616
private Api api;
1717
private Page page;
1818
private WebSocket webSocket;
19+
private Trace trace;
1920

2021
@Data
2122
@AllArgsConstructor
2223
@JsonInclude(JsonInclude.Include.NON_NULL)
2324
public static class Page {
2425
@ApiModelProperty(value = "Page size", required = true)
2526
private int maxSize;
26-
@ApiModelProperty(value = "Totatl number data objects", required = true)
27+
@ApiModelProperty(value = "Total number data objects", required = true)
2728
private long total;
2829
@ApiModelProperty("Link to the previous page if exist")
2930
private String prev;
@@ -36,6 +37,26 @@ public static class Page {
3637
@AllArgsConstructor
3738
@JsonInclude(JsonInclude.Include.NON_NULL)
3839
public static class WebSocket {
40+
@ApiModelProperty("Websocket session id")
3941
private UUID sessionId;
4042
}
43+
44+
@Data
45+
@JsonInclude(JsonInclude.Include.NON_NULL)
46+
public static class Trace {
47+
public Trace() {
48+
49+
}
50+
51+
public Trace(final String id) {
52+
this.id = id;
53+
}
54+
55+
public Trace(final UUID id) {
56+
this.id = id.toString();
57+
}
58+
59+
@ApiModelProperty("Trace id (any string identifier, ie request id)")
60+
private String id;
61+
}
4162
}

src/main/java/com/slmdev/jsonapi/simple/response/Response.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public ResponseBuilder() {
8181
this.meta = new Meta(
8282
new Api(DEFAULT_API_VERSION),
8383
new Meta.Page(DEFAULT_MAX_PAGE_SIZE, -1, null, null),
84+
null,
8485
null
8586
);
8687
}
@@ -519,6 +520,16 @@ public ResponseBuilder<T, V> metaWebSocket(final @NonNull Meta.WebSocket metaWeb
519520
return this;
520521
}
521522

523+
/**
524+
* @param trace trace specific meta information
525+
* @return self link
526+
*/
527+
public ResponseBuilder<T, V> metaTrace(final @NonNull Meta.Trace trace) {
528+
meta.setTrace(trace);
529+
530+
return this;
531+
}
532+
522533
/**
523534
* Build response.
524535
*

src/test/java/com/slmdev/jsonapi/simple/response/ResponseTest.java

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,16 +289,52 @@ public void shouldReturnResponseWithManuallyDataTypeAndIdAndValidInvokesOrderFor
289289
assertThat(response.getData().getAttributes().get("key"), is(List.of("value")));
290290
}
291291

292+
@Test
293+
public void shouldReturnResponseWithTraceIdAsString() {
294+
final String id = UUID.randomUUID().toString();
295+
final String traceId = UUID.randomUUID().toString();
296+
final Response<Data<Map<String, List<String>>>> response = Response.<Data<Map<String, List<String>>>, Map<String, List<String>>>builder()
297+
.jsonApiId(id)
298+
.jsonApiType("custom-data-type")
299+
.data(Map.of("key", List.of("value")))
300+
.metaTrace(new Meta.Trace(traceId))
301+
.build();
302+
303+
assertThat(response.getData().getId(), is(id));
304+
assertThat(response.getData().getType(), is("custom-data-type"));
305+
assertThat(response.getData().getAttributes().get("key"), is(List.of("value")));
306+
307+
assertThat(response.getMeta().getApi().getVersion(), is("1"));
308+
assertThat(response.getMeta().getTrace().getId(), is(traceId));
309+
}
310+
311+
@Test
312+
public void shouldReturnResponseWithTraceIdAsUuid() {
313+
final String id = UUID.randomUUID().toString();
314+
final UUID traceId = UUID.randomUUID();
315+
final Response<Data<Map<String, List<String>>>> response = Response.<Data<Map<String, List<String>>>, Map<String, List<String>>>builder()
316+
.jsonApiId(id)
317+
.jsonApiType("custom-data-type")
318+
.data(Map.of("key", List.of("value")))
319+
.metaTrace(new Meta.Trace(traceId))
320+
.build();
321+
322+
assertThat(response.getData().getId(), is(id));
323+
assertThat(response.getData().getType(), is("custom-data-type"));
324+
assertThat(response.getData().getAttributes().get("key"), is(List.of("value")));
325+
326+
assertThat(response.getMeta().getApi().getVersion(), is("1"));
327+
assertThat(response.getMeta().getTrace().getId(), is(traceId.toString()));
328+
}
329+
292330
@Test
293331
public void shouldThrowExceptionWithManuallyDataTypeAndInValidInvokesOrder() {
294332
Assertions.assertThrows(
295333
RuntimeException.class,
296-
() -> {
297-
Response.<Data<Map<String, String>>, Map<String, String>>builder()
298-
.data(Map.of("key", "value"))
299-
.jsonApiType("custom-data-type")
300-
.build();
301-
}
334+
() -> Response.<Data<Map<String, String>>, Map<String, String>>builder()
335+
.data(Map.of("key", "value"))
336+
.jsonApiType("custom-data-type")
337+
.build()
302338
);
303339
}
304340

@@ -412,6 +448,39 @@ public void shouldParseJsonResponseWithErrors() {
412448
assertThat(response.getErrors().get(0).getSource().getParameter(), is("name"));
413449
}
414450

451+
@Test
452+
@SneakyThrows
453+
public void shouldParseJsonResponseWithMetaTrace() {
454+
final String traceId = UUID.randomUUID().toString();
455+
final String data = "{\n" +
456+
" \"errors\": [\n" +
457+
" {\n" +
458+
" \"code\": \"400\",\n" +
459+
" \"source\": {\n" +
460+
" \"parameter\": \"name\"\n" +
461+
" }\n" +
462+
" }\n" +
463+
" ],\n" +
464+
" \"meta\": {\n" +
465+
" \"api\": {\n" +
466+
" \"version\": \"1\"\n" +
467+
" },\n" +
468+
" \"trace\": {\n" +
469+
" \"id\": \"" + traceId + "\"\n" +
470+
" }\n" +
471+
" }\n" +
472+
"}";
473+
final TypeReference<Response<List<Data<Map<String, Object>>>>> type = new TypeReference<>() {};
474+
final Response<List<Data<Map<String, Object>>>> response = new ObjectMapper().readValue(data, type);
475+
476+
assertThat(response.getErrors().size(), is(1));
477+
assertThat(response.getErrors().get(0).getCode(), is("400"));
478+
assertThat(response.getErrors().get(0).getSource().getParameter(), is("name"));
479+
480+
assertThat(response.getMeta().getApi().getVersion(), is("1"));
481+
assertThat(response.getMeta().getTrace().getId(), is(traceId));
482+
}
483+
415484
private TestDto buildTestDto1() {
416485
return new TestDto()
417486
.setId(TEST_DTO_1_ID)

0 commit comments

Comments
 (0)