Skip to content

Commit 90d25ba

Browse files
authored
Merge pull request #47 from daglem/status-response-cleanup
Clean up status responses
2 parents c231ae2 + 65e9a78 commit 90d25ba

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/main/java/com/mservicetech/openapi/validation/OpenApiValidator.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class OpenApiValidator {
5454
final String VALIDATOR_REQUEST_PARAMETER_QUERY_MISSING = "ERR11000";
5555

5656
final String VALIDATOR_RESPONSE_CONTENT_UNEXPECTED = "ERR11018";
57-
final String REQUIRED_RESPONSE_HEADER_MISSING = "ERR11019";
57+
final String VALIDATOR_RESPONSE_HEADER_MISSING = "ERR11020";
5858

5959
final String DEFAULT_STATUS_CODE = "default";
6060

@@ -139,7 +139,7 @@ public Status validateRequestPath (String requestURI , String httpMethod, Reques
139139
}
140140
} catch (NullPointerException e) {
141141
// This exception is thrown from within the OpenApiOperation constructor on operation == null.
142-
return new Status(STATUS_METHOD_NOT_ALLOWED, requestURI);
142+
return new Status(STATUS_METHOD_NOT_ALLOWED, httpMethod, requestURI);
143143
}
144144

145145
if (requestEntity!=null) {
@@ -446,7 +446,7 @@ public Status validateResponsePath(String requestURI, String httpMethod, String
446446
}
447447
} catch (NullPointerException e) {
448448
// This exception is thrown from within the OpenApiOperation constructor on operation == null.
449-
return new Status(STATUS_METHOD_NOT_ALLOWED, requestURI);
449+
return new Status(STATUS_METHOD_NOT_ALLOWED, httpMethod, requestURI);
450450
}
451451
Status status = validateHeaders(responseEntity.getHeaders(), openApiOperation, statusCode);
452452
if(status != null) return status;
@@ -462,7 +462,7 @@ private Status validateHeaders(Map<String, ?> headers, OpenApiOperation operatio
462462
//based on OpenAPI specification, ignore "Content-Type" header
463463
//If a response header is defined with the name "Content-Type", it SHALL be ignored. - https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#responseObject
464464
.filter(entry -> !"Content-Type".equalsIgnoreCase(entry.getKey()))
465-
.map(p -> validateHeader(headers, p.getKey(), p.getValue()))
465+
.map(p -> validateHeader(headers, p.getKey(), p.getValue(), operation))
466466
.filter(s -> s != null)
467467
.findFirst();
468468
if(optional.isPresent()) {
@@ -472,7 +472,7 @@ private Status validateHeaders(Map<String, ?> headers, OpenApiOperation operatio
472472
return null;
473473
}
474474

475-
private Status validateHeader(Map<String, ?> headers, String headerName, Header operationHeader) {
475+
private Status validateHeader(Map<String, ?> headers, String headerName, Header operationHeader, OpenApiOperation openApiOperation) {
476476
// According to RFC7230, header field names are case-insensitive.
477477
Optional<Object> headerValue = Optional.ofNullable(headers).flatMap(opt -> opt.entrySet()
478478
.stream()
@@ -482,7 +482,7 @@ private Status validateHeader(Map<String, ?> headers, String headerName, Header
482482
);
483483
if (headerValue.isEmpty()) {
484484
if (Boolean.TRUE.equals(operationHeader.getRequired())) {
485-
return new Status(REQUIRED_RESPONSE_HEADER_MISSING, headerName);
485+
return new Status(VALIDATOR_RESPONSE_HEADER_MISSING, headerName, openApiOperation.getPathString().original());
486486
}
487487
} else {
488488
SchemaValidatorsConfig config = new SchemaValidatorsConfig();

src/main/resources/status.yml

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ ERR10008:
166166
statusCode: 405
167167
code: ERR10008
168168
message: METHOD_NOT_ALLOWED
169-
description: Request method cannot be found in the spec.
169+
description: Request method %s cannot be found in the path %s.
170170
ERR10009:
171171
statusCode: 408
172172
code: ERR10009
@@ -366,7 +366,7 @@ ERR10048:
366366
statusCode: 404
367367
code: ERR10048
368368
message: PATH_NOT_IMPLEMENTED
369-
description: No handler defined for path '%s'
369+
description: No handler defined for path '%s' and method %s
370370
ERR10049:
371371
statusCode: 403
372372
code: ERR10049
@@ -433,7 +433,7 @@ ERR11000:
433433
statusCode: 400
434434
code: ERR11000
435435
message: VALIDATOR_REQUEST_PARAMETER_QUERY_MISSING
436-
description: Query parameter %s is required, but not found in request.
436+
description: Query parameter %s is required on path %s but not found in request.
437437
ERR11001:
438438
statusCode: 400
439439
code: ERR11001
@@ -505,25 +505,35 @@ ERR11014:
505505
message: VALIDATOR_REQUEST_BODY_MISSING
506506
description: Method %s on path %s requires a request body. None found.
507507
ERR11015:
508-
statusCode: 400
508+
statusCode: 500
509509
code: ERR11015
510510
message: VALIDATOR_RESPONSE_STATUS_UNKNOWN
511511
description: Response status %d not defined for path %s.
512512
ERR11016:
513-
statusCode: 400
513+
statusCode: 500
514514
code: ERR11016
515515
message: VALIDATOR_RESPONSE_BODY_MISSING
516516
description: Method %s on path %s defines a response schema but no response body found.
517517
ERR11017:
518518
statusCode: 400
519519
code: ERR11017
520520
message: VALIDATOR_REQUEST_PARAMETER_HEADER_MISSING
521-
description: Header parameter %s is required, but not found in request.
521+
description: Header parameter %s is required on path %s but not found in request.
522522
ERR11018:
523-
statusCode: 400
523+
statusCode: 500
524524
code: ERR11018
525525
message: VALIDATOR_RESPONSE_CONTENT_UNEXPECTED
526526
description: No response body content or schema is expected for %s on path %s.
527+
ERR11019:
528+
statusCode: 400
529+
code: ERR11019
530+
message: STARTUP_HOOK_NOT_LOADED
531+
description: Startup hook %s is not loaded properly.
532+
ERR11020:
533+
statusCode: 500
534+
code: ERR11020
535+
message: VALIDATOR_RESPONSE_HEADER_MISSING
536+
description: Response header %s is required on path %s but is not found.
527537

528538
# 11100-11199 Light Code Generator validation errors
529539
ERR11100:

0 commit comments

Comments
 (0)