Skip to content

Commit cc8cb0b

Browse files
committed
Add media type parser and mediaType validation
A media type parser is needed to parse Smithy mediaType traits both for codegen and also for validation.
1 parent 0c7be9e commit cc8cb0b

File tree

6 files changed

+563
-0
lines changed

6 files changed

+563
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.smithy.model.validation.validators;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.Optional;
21+
import software.amazon.smithy.model.Model;
22+
import software.amazon.smithy.model.shapes.Shape;
23+
import software.amazon.smithy.model.traits.MediaTypeTrait;
24+
import software.amazon.smithy.model.validation.AbstractValidator;
25+
import software.amazon.smithy.model.validation.ValidationEvent;
26+
import software.amazon.smithy.utils.MediaType;
27+
28+
public final class MediaTypeValidator extends AbstractValidator {
29+
@Override
30+
public List<ValidationEvent> validate(Model model) {
31+
List<ValidationEvent> events = new ArrayList<>();
32+
for (Shape shape : model.getShapesWithTrait(MediaTypeTrait.class)) {
33+
validateMediaType(shape, shape.expectTrait(MediaTypeTrait.class)).ifPresent(events::add);
34+
}
35+
36+
return events;
37+
}
38+
39+
private Optional<ValidationEvent> validateMediaType(Shape shape, MediaTypeTrait trait) {
40+
try {
41+
MediaType.from(trait.getValue());
42+
return Optional.empty();
43+
} catch (RuntimeException e) {
44+
return Optional.of(error(shape, trait, String.format(
45+
"Invalid mediaType value, \"%s\": %s", trait.getValue(), e.getMessage())));
46+
}
47+
}
48+
}

smithy-model/src/main/resources/META-INF/services/software.amazon.smithy.model.validation.Validator

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ software.amazon.smithy.model.validation.validators.HttpQueryTraitValidator
1515
software.amazon.smithy.model.validation.validators.HttpResponseCodeSemanticsValidator
1616
software.amazon.smithy.model.validation.validators.HttpUriConflictValidator
1717
software.amazon.smithy.model.validation.validators.LengthTraitValidator
18+
software.amazon.smithy.model.validation.validators.MediaTypeValidator
1819
software.amazon.smithy.model.validation.validators.NoInlineDocumentSupportValidator
1920
software.amazon.smithy.model.validation.validators.PaginatedTraitValidator
2021
software.amazon.smithy.model.validation.validators.PrivateAccessValidator
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[ERROR] smithy.example#Invalid1: Invalid mediaType value, " | MediaType
2+
[ERROR] smithy.example#Invalid2: Invalid mediaType value, " | MediaType
3+
[ERROR] smithy.example#Invalid3: Invalid mediaType value, " | MediaType
4+
[ERROR] smithy.example#Invalid4: Invalid mediaType value, " | MediaType
5+
[ERROR] smithy.example#Invalid5: Invalid mediaType value, " | MediaType
6+
[ERROR] smithy.example#Invalid6: Invalid mediaType value, " | MediaType
7+
[ERROR] smithy.example#Invalid7: Invalid mediaType value, " | MediaType
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"smithy": "1.0",
3+
"shapes": {
4+
"smithy.example#Valid1": {
5+
"type": "string",
6+
"traits": {
7+
"smithy.api#mediaType": "application/json"
8+
}
9+
},
10+
"smithy.example#Valid2": {
11+
"type": "string",
12+
"traits": {
13+
"smithy.api#mediaType": "application/foo+json"
14+
}
15+
},
16+
"smithy.example#Valid3": {
17+
"type": "string",
18+
"traits": {
19+
"smithy.api#mediaType": "application/foo+json; bar=baz"
20+
}
21+
},
22+
"smithy.example#Valid4": {
23+
"type": "string",
24+
"traits": {
25+
"smithy.api#mediaType": "application/foo+json; bar=baz; bam=boozled; foo=\"hi\""
26+
}
27+
},
28+
"smithy.example#Invalid1": {
29+
"type": "string",
30+
"traits": {
31+
"smithy.api#mediaType": "application"
32+
}
33+
},
34+
"smithy.example#Invalid2": {
35+
"type": "string",
36+
"traits": {
37+
"smithy.api#mediaType": "application/"
38+
}
39+
},
40+
"smithy.example#Invalid3": {
41+
"type": "string",
42+
"traits": {
43+
"smithy.api#mediaType": "application/json;"
44+
}
45+
},
46+
"smithy.example#Invalid4": {
47+
"type": "string",
48+
"traits": {
49+
"smithy.api#mediaType": "application/json; bar"
50+
}
51+
},
52+
"smithy.example#Invalid5": {
53+
"type": "string",
54+
"traits": {
55+
"smithy.api#mediaType": "application/json; bar="
56+
}
57+
},
58+
"smithy.example#Invalid6": {
59+
"type": "string",
60+
"traits": {
61+
"smithy.api#mediaType": "application/json; bar=,"
62+
}
63+
},
64+
"smithy.example#Invalid7": {
65+
"type": "string",
66+
"traits": {
67+
"smithy.api#mediaType": "application/json; bar=bam;"
68+
}
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)