Skip to content

Commit

Permalink
Add media type parser and mediaType validation
Browse files Browse the repository at this point in the history
A media type parser is needed to parse Smithy mediaType traits both for
codegen and also for validation.
  • Loading branch information
mtdowling committed Sep 29, 2020
1 parent 0c7be9e commit cc8cb0b
Show file tree
Hide file tree
Showing 6 changed files with 563 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.smithy.model.validation.validators;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.traits.MediaTypeTrait;
import software.amazon.smithy.model.validation.AbstractValidator;
import software.amazon.smithy.model.validation.ValidationEvent;
import software.amazon.smithy.utils.MediaType;

public final class MediaTypeValidator extends AbstractValidator {
@Override
public List<ValidationEvent> validate(Model model) {
List<ValidationEvent> events = new ArrayList<>();
for (Shape shape : model.getShapesWithTrait(MediaTypeTrait.class)) {
validateMediaType(shape, shape.expectTrait(MediaTypeTrait.class)).ifPresent(events::add);
}

return events;
}

private Optional<ValidationEvent> validateMediaType(Shape shape, MediaTypeTrait trait) {
try {
MediaType.from(trait.getValue());
return Optional.empty();
} catch (RuntimeException e) {
return Optional.of(error(shape, trait, String.format(
"Invalid mediaType value, \"%s\": %s", trait.getValue(), e.getMessage())));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ software.amazon.smithy.model.validation.validators.HttpQueryTraitValidator
software.amazon.smithy.model.validation.validators.HttpResponseCodeSemanticsValidator
software.amazon.smithy.model.validation.validators.HttpUriConflictValidator
software.amazon.smithy.model.validation.validators.LengthTraitValidator
software.amazon.smithy.model.validation.validators.MediaTypeValidator
software.amazon.smithy.model.validation.validators.NoInlineDocumentSupportValidator
software.amazon.smithy.model.validation.validators.PaginatedTraitValidator
software.amazon.smithy.model.validation.validators.PrivateAccessValidator
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[ERROR] smithy.example#Invalid1: Invalid mediaType value, " | MediaType
[ERROR] smithy.example#Invalid2: Invalid mediaType value, " | MediaType
[ERROR] smithy.example#Invalid3: Invalid mediaType value, " | MediaType
[ERROR] smithy.example#Invalid4: Invalid mediaType value, " | MediaType
[ERROR] smithy.example#Invalid5: Invalid mediaType value, " | MediaType
[ERROR] smithy.example#Invalid6: Invalid mediaType value, " | MediaType
[ERROR] smithy.example#Invalid7: Invalid mediaType value, " | MediaType
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"smithy": "1.0",
"shapes": {
"smithy.example#Valid1": {
"type": "string",
"traits": {
"smithy.api#mediaType": "application/json"
}
},
"smithy.example#Valid2": {
"type": "string",
"traits": {
"smithy.api#mediaType": "application/foo+json"
}
},
"smithy.example#Valid3": {
"type": "string",
"traits": {
"smithy.api#mediaType": "application/foo+json; bar=baz"
}
},
"smithy.example#Valid4": {
"type": "string",
"traits": {
"smithy.api#mediaType": "application/foo+json; bar=baz; bam=boozled; foo=\"hi\""
}
},
"smithy.example#Invalid1": {
"type": "string",
"traits": {
"smithy.api#mediaType": "application"
}
},
"smithy.example#Invalid2": {
"type": "string",
"traits": {
"smithy.api#mediaType": "application/"
}
},
"smithy.example#Invalid3": {
"type": "string",
"traits": {
"smithy.api#mediaType": "application/json;"
}
},
"smithy.example#Invalid4": {
"type": "string",
"traits": {
"smithy.api#mediaType": "application/json; bar"
}
},
"smithy.example#Invalid5": {
"type": "string",
"traits": {
"smithy.api#mediaType": "application/json; bar="
}
},
"smithy.example#Invalid6": {
"type": "string",
"traits": {
"smithy.api#mediaType": "application/json; bar=,"
}
},
"smithy.example#Invalid7": {
"type": "string",
"traits": {
"smithy.api#mediaType": "application/json; bar=bam;"
}
}
}
}
Loading

0 comments on commit cc8cb0b

Please sign in to comment.