Skip to content

Commit f3e8989

Browse files
committed
Add new method to parse errortype from document, move error sanitizer to documentutils and add sanitizer for cbor
1 parent 450b007 commit f3e8989

File tree

11 files changed

+102
-64
lines changed

11 files changed

+102
-64
lines changed

aws/client/aws-client-awsjson/src/main/java/software/amazon/smithy/java/aws/client/awsjson/AwsJson11Protocol.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import software.amazon.smithy.java.client.core.ClientProtocol;
1111
import software.amazon.smithy.java.client.core.ClientProtocolFactory;
1212
import software.amazon.smithy.java.client.core.ProtocolSettings;
13-
import software.amazon.smithy.java.json.ErrorTypeSanitizer;
13+
import software.amazon.smithy.java.core.serde.document.DocumentUtils;
1414
import software.amazon.smithy.model.shapes.ShapeId;
1515

1616
/**
@@ -25,7 +25,7 @@ public final class AwsJson11Protocol extends AwsJsonProtocol {
2525
* discriminator of documents that use relative shape IDs.
2626
*/
2727
public AwsJson11Protocol(ShapeId service) {
28-
super(TRAIT_ID, service, ErrorTypeSanitizer::removeNamespaceAndUri);
28+
super(TRAIT_ID, service, DocumentUtils::removeNamespaceAndUri);
2929
}
3030

3131
@Override

aws/client/aws-client-awsjson/src/main/java/software/amazon/smithy/java/aws/client/awsjson/AwsJson1Protocol.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import software.amazon.smithy.java.client.core.ClientProtocol;
1111
import software.amazon.smithy.java.client.core.ClientProtocolFactory;
1212
import software.amazon.smithy.java.client.core.ProtocolSettings;
13-
import software.amazon.smithy.java.json.ErrorTypeSanitizer;
13+
import software.amazon.smithy.java.core.serde.document.DocumentUtils;
1414
import software.amazon.smithy.model.shapes.ShapeId;
1515

1616
/**
@@ -25,7 +25,7 @@ public final class AwsJson1Protocol extends AwsJsonProtocol {
2525
* discriminator of documents that use relative shape IDs.
2626
*/
2727
public AwsJson1Protocol(ShapeId service) {
28-
super(TRAIT_ID, service, ErrorTypeSanitizer::removeUri);
28+
super(TRAIT_ID, service, DocumentUtils::removeUri);
2929
}
3030

3131
@Override

aws/client/aws-client-restjson/src/main/java/software/amazon/smithy/java/aws/client/restjson/RestJsonClientProtocol.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
import software.amazon.smithy.java.core.schema.SerializableStruct;
2525
import software.amazon.smithy.java.core.schema.TraitKey;
2626
import software.amazon.smithy.java.core.serde.Codec;
27+
import software.amazon.smithy.java.core.serde.document.DocumentUtils;
2728
import software.amazon.smithy.java.core.serde.event.EventDecoderFactory;
2829
import software.amazon.smithy.java.core.serde.event.EventEncoderFactory;
2930
import software.amazon.smithy.java.core.serde.event.EventStreamingException;
3031
import software.amazon.smithy.java.http.api.HttpRequest;
3132
import software.amazon.smithy.java.http.binding.RequestSerializer;
32-
import software.amazon.smithy.java.json.ErrorTypeSanitizer;
3333
import software.amazon.smithy.java.json.JsonCodec;
3434
import software.amazon.smithy.model.shapes.ShapeId;
3535
import software.amazon.smithy.model.shapes.ShapeType;
@@ -53,7 +53,7 @@ public RestJsonClientProtocol(ShapeId service) {
5353
.useJsonName(true)
5454
.useTimestampFormat(true)
5555
.defaultNamespace(service.getNamespace())
56-
.errorTypeSanitizer(ErrorTypeSanitizer::removeNamespaceAndUri)
56+
.errorTypeSanitizer(DocumentUtils::removeNamespaceAndUri)
5757
.build();
5858

5959
this.errorDeserializer = HttpErrorDeserializer.builder()

client/client-http/src/main/java/software/amazon/smithy/java/client/http/HttpErrorDeserializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ private static CallException makeErrorFromPayload(
252252

253253
if (buffer.remaining() > 0) {
254254
var document = codec.createDeserializer(buffer).readDocument();
255-
var id = document.discriminator();
255+
var id = document.parseErrorType();
256256
var builder = typeRegistry.createBuilder(id, ModeledException.class);
257257
if (builder != null) {
258258
return knownErrorFactory.createErrorFromDocument(

codecs/cbor-codec/src/main/java/software/amazon/smithy/java/cbor/CborDocuments.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import software.amazon.smithy.java.core.serde.ShapeSerializer;
1414
import software.amazon.smithy.java.core.serde.document.Document;
1515
import software.amazon.smithy.java.core.serde.document.DocumentDeserializer;
16+
import software.amazon.smithy.java.core.serde.document.DocumentUtils;
1617
import software.amazon.smithy.model.shapes.ShapeId;
1718
import software.amazon.smithy.model.shapes.ShapeType;
1819
import software.amazon.smithy.utils.SmithyInternalApi;
@@ -51,11 +52,13 @@ public ShapeType type() {
5152

5253
@Override
5354
public ShapeId discriminator() {
54-
String discriminator = null;
55-
var member = values.get("__type");
56-
if (member != null && member.type() == ShapeType.STRING) {
57-
discriminator = member.asString();
58-
}
55+
var discriminator = extractType();
56+
return DocumentDeserializer.parseDiscriminator(discriminator, settings.defaultNamespace());
57+
}
58+
59+
@Override
60+
public ShapeId parseErrorType() {
61+
var discriminator = DocumentUtils.removeUri(extractType());
5962
return DocumentDeserializer.parseDiscriminator(discriminator, settings.defaultNamespace());
6063
}
6164

@@ -102,6 +105,14 @@ public boolean equals(Object obj) {
102105
public int hashCode() {
103106
return values.hashCode();
104107
}
108+
109+
private String extractType() {
110+
var member = values.get("__type");
111+
if (member != null && member.type() == ShapeType.STRING) {
112+
return member.asString();
113+
}
114+
return null;
115+
}
105116
}
106117

107118
/**

codecs/json-codec/src/main/java/software/amazon/smithy/java/json/ErrorTypeSanitizer.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

codecs/json-codec/src/main/java/software/amazon/smithy/java/json/JsonDocuments.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,19 +246,29 @@ public ShapeType type() {
246246
@Override
247247
public ShapeId discriminator() {
248248
String discriminator = null;
249+
var member = values.get("__type");
250+
if (member != null && member.type() == ShapeType.STRING) {
251+
discriminator = member.asString();
252+
}
253+
return DocumentDeserializer.parseDiscriminator(discriminator, settings.defaultNamespace());
254+
}
255+
256+
@Override
257+
public ShapeId parseErrorType() {
258+
String errorType = null;
249259
var typeMember = values.get("__type");
250260
if (typeMember != null && typeMember.type() == ShapeType.STRING) {
251-
discriminator = typeMember.asString();
261+
errorType = typeMember.asString();
252262
} else {
253263
var codeMember = values.get("code");
254264
if (codeMember != null && codeMember.type() == ShapeType.STRING) {
255-
discriminator = codeMember.asString();
265+
errorType = codeMember.asString();
256266
}
257267
}
258268
if (settings.errorTypeSanitizer() != null) {
259-
discriminator = settings.errorTypeSanitizer().apply(discriminator);
269+
errorType = settings.errorTypeSanitizer().apply(errorType);
260270
}
261-
return DocumentDeserializer.parseDiscriminator(discriminator, settings.defaultNamespace());
271+
return DocumentDeserializer.parseDiscriminator(errorType, settings.defaultNamespace());
262272
}
263273

264274
@Override

codecs/json-codec/src/main/java/software/amazon/smithy/java/json/JsonSettings.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ public boolean serializeTypeInDocuments() {
119119
}
120120

121121
/**
122-
* The error type sanitizer to use for {@code __type}. Default is null
122+
* The error type sanitizer to use for {@code __type} or {@code code} when parsing the error discriminator.
123+
* Default is null
123124
*
124125
* @return the sanitizer used or null
125126
*/

core/src/main/java/software/amazon/smithy/java/core/serde/document/Document.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,18 @@ default ShapeId discriminator() {
125125
return null;
126126
}
127127

128+
/**
129+
* Attempts to find and parse a sanitized shape ID from the document in the document's discriminator field.
130+
*
131+
* <p> The discriminator of an error can be represented by either the __type field or the code field
132+
* based on the protocol.
133+
*
134+
* @return the parsed shape ID, or null if not found.
135+
*/
136+
default ShapeId parseErrorType() {
137+
return null;
138+
}
139+
128140
/**
129141
* Serializes the Document as a document value in the Smithy data model.
130142
*

core/src/main/java/software/amazon/smithy/java/core/serde/document/DocumentUtils.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,44 @@ public static <T> T getMemberValue(Document container, Schema containerSchema, S
175175
.id() + "`: " + e.getMessage());
176176
}
177177
}
178+
179+
/**
180+
* Removes the trailing URI in {@code __type} field or {@code code} field of the document
181+
*
182+
* <p>For example, given {@code __type = "aws.protocoltests.restjson#FooError:http://abc.com"},
183+
* protocols like restJSON should ignore the trailing URI and keep the namespace of the error type.
184+
*
185+
* @param text The error type string.
186+
* @return The error type string without the trailing URI.
187+
*/
188+
public static String removeUri(String text) {
189+
if (text == null) {
190+
return null;
191+
}
192+
var colon = text.indexOf(':');
193+
if (colon > 0) {
194+
text = text.substring(0, colon);
195+
}
196+
return text;
197+
}
198+
199+
/**
200+
* Removes the namespace and trailing URI in {@code __type} field or {@code code} field of the document
201+
*
202+
* <p>For example, given {@code __type = "aws.protocoltests.restjson#FooError:http://abc.com"},
203+
* protocols like awsJSON 1.1 should ignore the namespace and the trailing URI of the error type.
204+
*
205+
* @param text The error type string.
206+
* @return The error type string without the trailing URI.
207+
*/
208+
public static String removeNamespaceAndUri(String text) {
209+
if (text == null) {
210+
return null;
211+
}
212+
var hash = text.indexOf('#');
213+
if (hash > 0) {
214+
text = text.substring(hash + 1);
215+
}
216+
return removeUri(text);
217+
}
178218
}

0 commit comments

Comments
 (0)