From d9f7feae0507c4942b1752a428cf55e2440e0bcf Mon Sep 17 00:00:00 2001 From: JordonPhillips Date: Fri, 4 Dec 2020 13:40:15 +0100 Subject: [PATCH] Set OpenAPI requestBody to required if necessary This updates the OpenAPI conversion to set an operation's `requestBody` as required if the operation has an `httpPayload` member and that member is required or if the operation has no `httpPayload` member but does have a required member bound to the document. --- .../aws/apigateway/openapi/payloads.openapi.json | 3 ++- .../fromsmithy/protocols/AbstractRestProtocol.java | 11 +++++++++++ .../fromsmithy/test-service-integer.openapi.json | 14 +++++++++++--- .../smithy/openapi/fromsmithy/test-service.json | 9 ++++++++- .../openapi/fromsmithy/test-service.openapi.json | 14 +++++++++++--- 5 files changed, 43 insertions(+), 8 deletions(-) diff --git a/smithy-aws-apigateway-openapi/src/test/resources/software/amazon/smithy/aws/apigateway/openapi/payloads.openapi.json b/smithy-aws-apigateway-openapi/src/test/resources/software/amazon/smithy/aws/apigateway/openapi/payloads.openapi.json index 9165d5062d1..893217a4f3b 100644 --- a/smithy-aws-apigateway-openapi/src/test/resources/software/amazon/smithy/aws/apigateway/openapi/payloads.openapi.json +++ b/smithy-aws-apigateway-openapi/src/test/resources/software/amazon/smithy/aws/apigateway/openapi/payloads.openapi.json @@ -15,7 +15,8 @@ "$ref": "#/components/schemas/FooOperationPayload" } } - } + }, + "required": true }, "parameters": [ { diff --git a/smithy-openapi/src/main/java/software/amazon/smithy/openapi/fromsmithy/protocols/AbstractRestProtocol.java b/smithy-openapi/src/main/java/software/amazon/smithy/openapi/fromsmithy/protocols/AbstractRestProtocol.java index 9f11884ea83..f37e0458aeb 100644 --- a/smithy-openapi/src/main/java/software/amazon/smithy/openapi/fromsmithy/protocols/AbstractRestProtocol.java +++ b/smithy-openapi/src/main/java/software/amazon/smithy/openapi/fromsmithy/protocols/AbstractRestProtocol.java @@ -300,6 +300,7 @@ private Optional createRequestPayload( }); RequestBodyObject requestBodyObject = RequestBodyObject.builder() .putContent(Objects.requireNonNull(mediaTypeRange), mediaTypeObject) + .required(binding.getMember().isRequired()) .build(); return Optional.of(requestBodyObject); } @@ -325,8 +326,18 @@ private Optional createRequestDocument( .schema(Schema.builder().ref(pointer).build()) .build(); + // If any of the top level bindings are required, then the body itself must be required. + boolean required = false; + for (HttpBinding binding : bindings) { + if (binding.getMember().isRequired()) { + required = true; + break; + } + } + return Optional.of(RequestBodyObject.builder() .putContent(mediaType, mediaTypeObject) + .required(required) .build()); } diff --git a/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/test-service-integer.openapi.json b/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/test-service-integer.openapi.json index 59b6505f5aa..74e9189add8 100644 --- a/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/test-service-integer.openapi.json +++ b/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/test-service-integer.openapi.json @@ -15,7 +15,8 @@ "$ref": "#/components/schemas/CreateDocumentRequestContent" } } - } + }, + "required": true }, "parameters": [ { @@ -61,7 +62,8 @@ "$ref": "#/components/schemas/PutPayloadInputPayload" } } - } + }, + "required": true }, "parameters": [ { @@ -147,8 +149,14 @@ "stringDateTime": { "type": "string", "format": "date-time" + }, + "required": { + "type": "string" } - } + }, + "required": [ + "required" + ] }, "CreateDocumentResponseContent": { "type": "object", diff --git a/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/test-service.json b/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/test-service.json index 97d9eeb14b1..fec89bb4b61 100644 --- a/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/test-service.json +++ b/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/test-service.json @@ -75,7 +75,8 @@ "body": { "target": "example.rest#Blob", "traits": { - "smithy.api#httpPayload": {} + "smithy.api#httpPayload": {}, + "smithy.api#required": {} } } } @@ -142,6 +143,12 @@ }, "stringDateTime": { "target": "example.rest#StringDateTime" + }, + "required": { + "target": "example.rest#String", + "traits": { + "smithy.api#required": {} + } } } }, diff --git a/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/test-service.openapi.json b/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/test-service.openapi.json index 77de71895de..5cc3ed43cd3 100644 --- a/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/test-service.openapi.json +++ b/smithy-openapi/src/test/resources/software/amazon/smithy/openapi/fromsmithy/test-service.openapi.json @@ -15,7 +15,8 @@ "$ref": "#/components/schemas/CreateDocumentRequestContent" } } - } + }, + "required": true }, "parameters": [ { @@ -61,7 +62,8 @@ "$ref": "#/components/schemas/PutPayloadInputPayload" } } - } + }, + "required": true }, "parameters": [ { @@ -147,8 +149,14 @@ "stringDateTime": { "type": "string", "format": "date-time" + }, + "required": { + "type": "string" } - } + }, + "required": [ + "required" + ] }, "CreateDocumentResponseContent": { "type": "object",