Skip to content

Commit 6f29d42

Browse files
committed
fix: also return a response body in case of successful validation
1 parent d7a9786 commit 6f29d42

2 files changed

Lines changed: 35 additions & 27 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ build/
3535

3636
### Mac OS ###
3737
.DS_Store
38+
39+
# env files
40+
.env

src/main/java/massbank_export_api/api/ValidateApiDelegateImpl.java

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.springframework.stereotype.Service;
1111

1212
import java.util.HashSet;
13-
import java.util.Map;
1413

1514
@Primary
1615
@Service
@@ -23,44 +22,50 @@ public ValidateApiDelegateImpl() {
2322
}
2423

2524
@Override
26-
public ResponseEntity<Void> validatePost(String body) {
25+
public ResponseEntity<Void> validatePost(final String body) {
2726
if (body == null || body.trim().isEmpty()) {
2827
return ResponseEntity.badRequest().build();
2928
}
3029

31-
Result result = recordParser.parse(body);
30+
ResponseEntity<ValidationError> respWithBody;
31+
final ValidationError error = new ValidationError();
32+
final Result result = recordParser.parse(body);
3233
if (result.isSuccess()) {
33-
return ResponseEntity.ok().build();
34-
}
34+
error.setMessage("Record is valid.");
35+
error.setLine(null);
36+
error.setColumn(null);
37+
38+
respWithBody = ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(error);
39+
} else {
3540

36-
String message = result.getMessage();
37-
int pos = result.getPosition();
41+
final String message = result.getMessage();
42+
final int pos = result.getPosition();
3843

39-
String[] tokens = body.split("\\n");
40-
int offset = 0;
41-
int lineNumber = 1;
42-
int col = 0;
43-
for (String token : tokens) {
44-
offset += token.length() + 1;
45-
if (pos < offset) {
46-
col = pos - (offset - (token.length() + 1));
47-
break;
44+
final String[] tokens = body.split("\\n");
45+
int offset = 0;
46+
int lineNumber = 1;
47+
int col = 0;
48+
for (String token : tokens) {
49+
offset += token.length() + 1;
50+
if (pos < offset) {
51+
col = pos - (offset - (token.length() + 1));
52+
break;
53+
}
54+
lineNumber++;
4855
}
49-
lineNumber++;
50-
}
5156

52-
ValidationError error = new ValidationError();
53-
error.setMessage(message);
54-
error.setLine(lineNumber);
55-
error.setColumn(col);
57+
error.setMessage(message);
58+
error.setLine(lineNumber);
59+
error.setColumn(col);
5660

57-
ResponseEntity<ValidationError> respWithBody =
58-
ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY)
59-
.contentType(MediaType.APPLICATION_JSON)
60-
.body(error);
61+
respWithBody = ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY)
62+
.contentType(MediaType.APPLICATION_JSON)
63+
.body(error);
64+
}
6165

6266
@SuppressWarnings("unchecked")
63-
ResponseEntity<Void> casted = (ResponseEntity<Void>)(ResponseEntity<?>) respWithBody;
67+
final ResponseEntity<Void> casted = (ResponseEntity<Void>) (ResponseEntity<?>) respWithBody;
68+
6469
return casted;
6570
}
6671

0 commit comments

Comments
 (0)