Skip to content

Commit a00f281

Browse files
committed
Update version to 1.8.0
Update packages dependencies Add error links object support Update doc
1 parent 6c5a7a4 commit a00f281

File tree

6 files changed

+140
-19
lines changed

6 files changed

+140
-19
lines changed

CHANGELOG.md

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

3+
## [1.8.0] - 2022-11-01
4+
### Added:
5+
- Error links object support
6+
7+
### Changed:
8+
- Update all dependencies versions
9+
310
## [1.7.0] - 2022-08-22
411
### Added:
512
- Methods to get typed params from filter (i.e. **stringValue**, **listOfUuidValues** etc.)

README.md

Lines changed: 48 additions & 4 deletions
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.7.0</version>
19+
<version>1.8.0</version>
2020
</dependency>
2121
```
2222

@@ -464,15 +464,15 @@ public class Test {
464464
}
465465
```
466466
467-
Example response with error (data is empty so we use ```Void``` class as generic parameter):
467+
Example response with error (data is empty, so we use ```Void``` class as generic parameter):
468468
```java
469469
// Pseudo code
470470
public class Test {
471471
public Response<Void> main() {
472472
return Response.<Void, Void>builder()
473473
.error(
474474
HttpStatus.BAD_REQUEST,
475-
"TOKEN_ERROR",
475+
"20045",
476476
"Some errors occurred!"
477477
).build();
478478
}
@@ -483,7 +483,7 @@ public class Test {
483483
"errors": [
484484
{
485485
"status": 400,
486-
"code": "TOKEN_ERROR",
486+
"code": "20045",
487487
"detail": "Some errors occurred!"
488488
}
489489
],
@@ -499,6 +499,50 @@ public class Test {
499499
}
500500
```
501501
502+
Example response with error with links (data is empty, so we use ```Void``` class as generic parameter):
503+
```java
504+
// Pseudo code
505+
public class Test {
506+
public Response<Void> main() {
507+
final Error.ErrorLink errorLink = Error.ErrorLink
508+
.builder()
509+
.about("https://example.org/docs/TOKEN_ERROR")
510+
.build();
511+
512+
return Response.<Void, Void>builder()
513+
.error(
514+
HttpStatus.BAD_REQUEST,
515+
"20045",
516+
"Some errors occurred!",
517+
errorLink
518+
).build();
519+
}
520+
}
521+
```
522+
```json
523+
{
524+
"errors": [
525+
{
526+
"status": 400,
527+
"code": "20045",
528+
"detail": "Some errors occurred!",
529+
"links": {
530+
"about": "https://example.org/docs/20045"
531+
}
532+
}
533+
],
534+
"meta": {
535+
"api": {
536+
"version": "1"
537+
},
538+
"page": {
539+
"maxSize": 25,
540+
"total": 0
541+
}
542+
}
543+
}
544+
```
545+
502546
Example response with validation error and field name with invalid data (data is empty so we use ```Void```
503547
class as generic parameter):
504548
```java

pom.xml

Lines changed: 7 additions & 6 deletions
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.7.0</version>
7+
<version>1.8.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>
@@ -35,15 +35,16 @@
3535
<properties>
3636
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3737
<java.version>11</java.version>
38-
<spring.web.version>5.3.22</spring.web.version>
39-
<spring.data.commons.version>2.7.2</spring.data.commons.version>
38+
<spring.web.version>5.3.23</spring.web.version>
39+
<spring.data.commons.version>2.7.5</spring.data.commons.version>
4040
<lombok.version>1.18.24</lombok.version>
4141
<lombok.maven.plugin.version>1.18.20.0</lombok.maven.plugin.version>
42-
<jackson.version>2.13.3</jackson.version>
42+
<jackson.version>2.13.4.2</jackson.version>
43+
<jackson.datatype.version>2.13.4</jackson.datatype.version>
4344
<sprinfox.version>3.0.0</sprinfox.version>
4445
<junit.version>5.9.0</junit.version>
4546
<hamcrest.version>2.2</hamcrest.version>
46-
<mockito.version>4.7.0</mockito.version>
47+
<mockito.version>4.8.0</mockito.version>
4748
<maven.compiler.version>3.10.1</maven.compiler.version>
4849
<maven.surefire.version>2.22.2</maven.surefire.version>
4950
<maven.deploy.version>2.8.2</maven.deploy.version>
@@ -81,7 +82,7 @@
8182
<dependency>
8283
<groupId>com.fasterxml.jackson.datatype</groupId>
8384
<artifactId>jackson-datatype-jsr310</artifactId>
84-
<version>${jackson.version}</version>
85+
<version>${jackson.datatype.version}</version>
8586
</dependency>
8687

8788
<dependency>

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import com.fasterxml.jackson.annotation.JsonInclude;
44
import io.swagger.annotations.ApiModelProperty;
5-
import lombok.AllArgsConstructor;
6-
import lombok.Getter;
7-
import lombok.NoArgsConstructor;
8-
import lombok.ToString;
5+
import lombok.*;
96
import lombok.experimental.Accessors;
107

118
@Getter
@@ -21,8 +18,9 @@ public class Error {
2118
private String code;
2219
@ApiModelProperty(value = "Error detail message", required = true)
2320
private String detail;
24-
@ApiModelProperty(value = "Parameter name only for validation errors", required = false)
21+
@ApiModelProperty(value = "Parameter name only for validation errors")
2522
private Source source;
23+
private ErrorLink links;
2624

2725
@Getter
2826
@ToString
@@ -33,4 +31,18 @@ public class Error {
3331
public static class Source {
3432
private String parameter;
3533
}
34+
35+
@Getter
36+
@ToString
37+
@Builder
38+
@NoArgsConstructor
39+
@AllArgsConstructor
40+
@Accessors(chain = true)
41+
@JsonInclude(JsonInclude.Include.NON_NULL)
42+
public static class ErrorLink {
43+
@ApiModelProperty(value = "A link that leads to further details about this particular occurrence of the problem")
44+
private String about;
45+
@ApiModelProperty(value = "A link that identifies the type of error that this particular error is an instance of")
46+
private String type;
47+
}
3648
}

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

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,8 @@ public ResponseBuilder<T, V> validationError(final @NonNull String errorValidati
345345
HttpStatus.BAD_REQUEST,
346346
"VALIDATION_ERROR",
347347
detail,
348-
errorValidationField
348+
errorValidationField,
349+
null
349350
);
350351
}
351352

@@ -357,7 +358,7 @@ public ResponseBuilder<T, V> validationError(final @NonNull String errorValidati
357358
* @return self link
358359
*/
359360
public ResponseBuilder<T, V> error(final HttpStatus status, final String detail) {
360-
return error(status, null, detail, null);
361+
return error(status, null, detail, null, null);
361362
}
362363

363364
/**
@@ -369,7 +370,7 @@ public ResponseBuilder<T, V> error(final HttpStatus status, final String detail)
369370
* @return self link
370371
*/
371372
public ResponseBuilder<T, V> error(final HttpStatus status, final String code, final String detail) {
372-
return error(status, code, detail, null);
373+
return error(status, code, detail, null, null);
373374
}
374375

375376
/**
@@ -388,6 +389,46 @@ public ResponseBuilder<T, V> error(final HttpStatus status,
388389
final String code,
389390
final @NonNull String detail,
390391
final String errorValidationField) {
392+
return error(status, code, detail, errorValidationField, null);
393+
}
394+
395+
/**
396+
* Create errors object.
397+
*
398+
* <p>We can invoke this method as many times as we need and
399+
* result object will contain all errors we passed.
400+
*
401+
* @param status spring {@link HttpStatus} object
402+
* @param code internal error code (if exists)
403+
* @param detail detail information about error
404+
* @param links field with links for the details about error
405+
* @return self link
406+
*/
407+
public ResponseBuilder<T, V> error(final HttpStatus status,
408+
final String code,
409+
final @NonNull String detail,
410+
final Error.ErrorLink links) {
411+
return error(status, code, detail, null, links);
412+
}
413+
414+
/**
415+
* Create errors object.
416+
*
417+
* <p>We can invoke this method as many times as we need and
418+
* result object will contain all errors we passed.
419+
*
420+
* @param status spring {@link HttpStatus} object
421+
* @param code internal error code (if exists)
422+
* @param detail detail information about error
423+
* @param errorValidationField field with failed validation
424+
* @param links field with links for the details about error
425+
* @return self link
426+
*/
427+
public ResponseBuilder<T, V> error(final HttpStatus status,
428+
final String code,
429+
final @NonNull String detail,
430+
final String errorValidationField,
431+
final Error.ErrorLink links) {
391432
if (this.errors == null) {
392433
this.errors = new ArrayList<>();
393434
}
@@ -397,7 +438,7 @@ public ResponseBuilder<T, V> error(final HttpStatus status,
397438
errorSource = new Error.Source(errorValidationField);
398439
}
399440
this.errors.add(
400-
new Error(status.value(), code, detail, errorSource)
441+
new Error(status.value(), code, detail, errorSource, links)
401442
);
402443
this.dataList = null;
403444
this.dataObject = null;

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,22 @@ public void shouldReturnResponseWithValidationErrorWithEmptyData() {
224224
assertThat(response.getErrors().get(0).getSource().getParameter(), is("someField"));
225225
}
226226

227+
@Test
228+
public void shouldReturnResponseWithErrorWithLinkAndWith() {
229+
final Error.ErrorLink errorLink = Error.ErrorLink
230+
.builder()
231+
.about("https://example.org/docs/" + ERROR_CODE)
232+
.build();
233+
234+
final Response<Void> response = Response.<Void, Void>builder()
235+
.error(HttpStatus.BAD_REQUEST, ERROR_CODE, ERROR_DESCRIPTION, errorLink)
236+
.build();
237+
238+
assertErrorResponse(response);
239+
240+
assertThat(response.getErrors().get(0).getLinks().getAbout(), is(errorLink.getAbout()));
241+
}
242+
227243
@Test
228244
public void shouldReturnResponseWithEmptyDataAndChangedMetaVersion() {
229245
final Response<Void> response = Response.<Void, Void>builder()

0 commit comments

Comments
 (0)