Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mediatypes #582

Merged
merged 1 commit into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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