Skip to content

Commit

Permalink
Warn when standard HTTP verb is mis-cased
Browse files Browse the repository at this point in the history
  • Loading branch information
kstich authored and alextwoods committed Sep 15, 2023
1 parent 6a67cb9 commit bf5df61
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import software.amazon.smithy.model.validation.AbstractValidator;
import software.amazon.smithy.model.validation.ValidationEvent;
import software.amazon.smithy.model.validation.ValidationUtils;
import software.amazon.smithy.utils.ListUtils;
import software.amazon.smithy.utils.MapUtils;

/**
Expand Down Expand Up @@ -94,10 +95,19 @@ private List<ValidationEvent> validateOperation(
HttpTrait trait
) {
String method = trait.getMethod().toUpperCase(Locale.US);
List<ValidationEvent> events = new ArrayList<>();

// We don't have a standard method, so we can't validate it.
if (!EXPECTED.containsKey(method)) {
return events;
return ListUtils.of();
}

List<ValidationEvent> events = new ArrayList<>();

// Emit a warning if the method is standard, but doesn't match standard casing.
if (!EXPECTED.containsKey(trait.getMethod())) {
events.add(warning(shape, trait, String.format(
"This operation uses the `%s` method in the `http` trait, but expected `%s`.",
trait.getMethod(), method)));
}

HttpMethodSemantics semantics = EXPECTED.get(method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
[WARNING] ns.foo#H: This operation uses the `DELETE` method in the `http` trait, but is not marked with the idempotent trait | HttpMethodSemantics.MissingIdempotentTrait
[WARNING] ns.foo#I: This operation uses the `GET` method in the `http` trait, but is not marked with the readonly trait | HttpMethodSemantics.MissingReadonlyTrait
[WARNING] ns.foo#Options: OPTIONS requests are typically used as part of CORS and should not be modeled explicitly. They are an implementation detail that should not appear in generated client or server code. Instead, tooling should use the `cors` trait on a service to automatically configure CORS on clients and servers. It is the responsibility of service frameworks and API gateways to automatically manage OPTIONS requests. For example, OPTIONS requests are automatically created when using Smithy models with Amazon API Gateway. | HttpMethodSemantics.OPTIONS
[WARNING] ns.foo#A: This operation uses the `Get` method in the `http` trait, but expected `GET`. | HttpMethodSemantics
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"traits": {
"smithy.api#readonly": {},
"smithy.api#http": {
"method": "GET",
"method": "Get",
"uri": "/A"
}
}
Expand Down

0 comments on commit bf5df61

Please sign in to comment.