|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +package software.amazon.smithy.openapi.fromsmithy.protocols; |
| 6 | + |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Optional; |
| 9 | +import java.util.Set; |
| 10 | +import java.util.TreeMap; |
| 11 | +import java.util.function.Function; |
| 12 | +import software.amazon.smithy.jsonschema.Schema; |
| 13 | +import software.amazon.smithy.model.Model; |
| 14 | +import software.amazon.smithy.model.knowledge.OperationIndex; |
| 15 | +import software.amazon.smithy.model.shapes.OperationShape; |
| 16 | +import software.amazon.smithy.model.shapes.Shape; |
| 17 | +import software.amazon.smithy.model.shapes.ShapeId; |
| 18 | +import software.amazon.smithy.model.shapes.StructureShape; |
| 19 | +import software.amazon.smithy.model.shapes.ToShapeId; |
| 20 | +import software.amazon.smithy.openapi.OpenApiConfig; |
| 21 | +import software.amazon.smithy.openapi.fromsmithy.Context; |
| 22 | +import software.amazon.smithy.openapi.fromsmithy.OpenApiProtocol; |
| 23 | +import software.amazon.smithy.openapi.model.MediaTypeObject; |
| 24 | +import software.amazon.smithy.openapi.model.OperationObject; |
| 25 | +import software.amazon.smithy.openapi.model.RequestBodyObject; |
| 26 | +import software.amazon.smithy.openapi.model.ResponseObject; |
| 27 | +import software.amazon.smithy.protocol.traits.Rpcv2JsonTrait; |
| 28 | +import software.amazon.smithy.utils.SetUtils; |
| 29 | + |
| 30 | +/** |
| 31 | + * Converts the {@code smithy.protocols#rpcv2Json} protocol to OpenAPI. |
| 32 | + * |
| 33 | + * <p>Each operation is mapped to a POST request with a path of |
| 34 | + * {@code /service/{serviceName}/operation/{operationName}}, where |
| 35 | + * {@code serviceName} is the service shape name (without namespace). |
| 36 | + */ |
| 37 | +public final class RpcV2JsonProtocolConverter implements OpenApiProtocol<Rpcv2JsonTrait> { |
| 38 | + private static final String STATUS_CODE = "200"; |
| 39 | + private static final String CONTENT_TYPE = "application/json"; |
| 40 | + |
| 41 | + private static final Set<String> REQUEST_HEADERS = SetUtils.of( |
| 42 | + // Since APIGW doesn't support event streaming, apply the 3 protocol headers. |
| 43 | + "Smithy-Protocol", |
| 44 | + "Content-Type", |
| 45 | + "Content-Length", |
| 46 | + // Used by clients for a purpose similar to the standard user-agent header. |
| 47 | + "X-Amz-User-Agent", |
| 48 | + // Used by clients configured to work with X-Ray. |
| 49 | + "X-Amzn-Trace-Id", |
| 50 | + // Used by clients for adaptive retry behavior. |
| 51 | + "Amz-Sdk-Request", |
| 52 | + "Amz-Sdk-Invocation-Id"); |
| 53 | + |
| 54 | + private static final Set<String> RESPONSE_HEADERS = SetUtils.of( |
| 55 | + // Since APIGW doesn't support event streaming, apply the 3 protocol headers. |
| 56 | + "Smithy-Protocol", |
| 57 | + "Content-Type", |
| 58 | + "Content-Length", |
| 59 | + // Used to identify a given request/response, primarily for debugging. |
| 60 | + "X-Amzn-Requestid"); |
| 61 | + |
| 62 | + @Override |
| 63 | + public Class<Rpcv2JsonTrait> getProtocolType() { |
| 64 | + return Rpcv2JsonTrait.class; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void updateDefaultSettings(Model model, OpenApiConfig config) { |
| 69 | + config.setUseStringsForArbitraryPrecision(true); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Each operation will have a separate path in the format /service/{serviceName}/operation/{operationName} |
| 74 | + */ |
| 75 | + @Override |
| 76 | + public Optional<OpenApiProtocol.Operation> createOperation( |
| 77 | + Context<Rpcv2JsonTrait> context, |
| 78 | + OperationShape operation |
| 79 | + ) { |
| 80 | + OperationObject.Builder builder = OperationObject.builder() |
| 81 | + .operationId(context.getService().getContextualName(operation)); |
| 82 | + createRequestBody(context, operation).ifPresent(builder::requestBody); |
| 83 | + createResponseBody(context, operation).forEach(builder::putResponse); |
| 84 | + return Optional.of(OpenApiProtocol.Operation.create( |
| 85 | + getOperationMethod(context, operation), |
| 86 | + getOperationUri(context, operation), |
| 87 | + builder)); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public String getOperationResponseStatusCode(Context<Rpcv2JsonTrait> context, ToShapeId operationOrError) { |
| 92 | + if (context.getModel().expectShape(operationOrError.toShapeId()).isOperationShape()) { |
| 93 | + return STATUS_CODE; |
| 94 | + } |
| 95 | + return OpenApiProtocol.super.getOperationResponseStatusCode(context, operationOrError); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public String getOperationMethod(Context<Rpcv2JsonTrait> context, OperationShape operation) { |
| 100 | + return "POST"; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public String getOperationUri(Context<Rpcv2JsonTrait> context, OperationShape operation) { |
| 105 | + return "/service/" + context.getService().getId().getName() |
| 106 | + + "/operation/" + context.getService().getContextualName(operation); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public Set<String> getProtocolRequestHeaders(Context<Rpcv2JsonTrait> context, OperationShape operationShape) { |
| 111 | + return REQUEST_HEADERS; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public Set<String> getProtocolResponseHeaders(Context<Rpcv2JsonTrait> context, OperationShape operationShape) { |
| 116 | + return RESPONSE_HEADERS; |
| 117 | + } |
| 118 | + |
| 119 | + private Map<String, ResponseObject> createResponseBody( |
| 120 | + Context<Rpcv2JsonTrait> context, |
| 121 | + OperationShape operation |
| 122 | + ) { |
| 123 | + Map<String, ResponseObject> result = new TreeMap<>(); |
| 124 | + |
| 125 | + if (operation.getOutput().isPresent()) { |
| 126 | + result.put(STATUS_CODE, |
| 127 | + buildResponseObject(context, |
| 128 | + operation.getOutputShape(), |
| 129 | + operation, |
| 130 | + "ResponseContent")); |
| 131 | + |
| 132 | + OperationIndex operationIndex = OperationIndex.of(context.getModel()); |
| 133 | + for (StructureShape error : operationIndex.getErrors(operation)) { |
| 134 | + String errorCode = context.getOpenApiProtocol().getOperationResponseStatusCode(context, error); |
| 135 | + result.put(errorCode, buildResponseObject(context, error.toShapeId(), error, "ErrorContent")); |
| 136 | + } |
| 137 | + } |
| 138 | + return result; |
| 139 | + } |
| 140 | + |
| 141 | + private ResponseObject buildResponseObject( |
| 142 | + Context<Rpcv2JsonTrait> context, |
| 143 | + ShapeId shape, |
| 144 | + Shape operationOrError, |
| 145 | + String suffix |
| 146 | + ) { |
| 147 | + Schema schema = convertToSchema(context, shape); |
| 148 | + MediaTypeObject mediaTypeObject = createMediaTypeObject(context, schema, operationOrError, suffix); |
| 149 | + |
| 150 | + return ResponseObject.builder() |
| 151 | + .description("Response Object") |
| 152 | + .putContent(CONTENT_TYPE, mediaTypeObject) |
| 153 | + .build(); |
| 154 | + } |
| 155 | + |
| 156 | + private Optional<RequestBodyObject> createRequestBody( |
| 157 | + Context<Rpcv2JsonTrait> context, |
| 158 | + OperationShape operation |
| 159 | + ) { |
| 160 | + if (operation.getInput().isPresent()) { |
| 161 | + Schema schema = convertToSchema(context, operation.getInputShape()); |
| 162 | + MediaTypeObject mediaTypeObject = createMediaTypeObject(context, schema, operation, "RequestContent"); |
| 163 | + |
| 164 | + return Optional.of(RequestBodyObject.builder() |
| 165 | + .description("Request Object") |
| 166 | + .putContent(CONTENT_TYPE, mediaTypeObject) |
| 167 | + .build()); |
| 168 | + } |
| 169 | + return Optional.empty(); |
| 170 | + } |
| 171 | + |
| 172 | + private MediaTypeObject createMediaTypeObject( |
| 173 | + Context<Rpcv2JsonTrait> context, |
| 174 | + Schema schema, |
| 175 | + Shape operationOrError, |
| 176 | + String suffix |
| 177 | + ) { |
| 178 | + return getMediaTypeObject(context, schema, operationOrError, shape -> { |
| 179 | + String shapeName = context.getService().getContextualName(shape.getId()); |
| 180 | + return shapeName + suffix; |
| 181 | + }); |
| 182 | + } |
| 183 | + |
| 184 | + private MediaTypeObject getMediaTypeObject( |
| 185 | + Context<Rpcv2JsonTrait> context, |
| 186 | + Schema schema, |
| 187 | + Shape shape, |
| 188 | + Function<Shape, String> createSynthesizedName |
| 189 | + ) { |
| 190 | + String synthesizedName = createSynthesizedName.apply(shape); |
| 191 | + String pointer = context.putSynthesizedSchema(synthesizedName, schema); |
| 192 | + return MediaTypeObject.builder() |
| 193 | + .schema(Schema.builder().ref(pointer).build()) |
| 194 | + .build(); |
| 195 | + } |
| 196 | + |
| 197 | + private Schema convertToSchema(Context<Rpcv2JsonTrait> context, ShapeId shape) { |
| 198 | + StructureShape containerShape = context.getModel().expectShape(shape, StructureShape.class); |
| 199 | + return context.getJsonSchemaConverter() |
| 200 | + .convertShape(containerShape) |
| 201 | + .getRootSchema(); |
| 202 | + } |
| 203 | +} |
0 commit comments