-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit fixes the httpApiKeyAuth trait so that it loads/serializes properly, it has tests, an it converts to OpenAPI properly. This commit also normalizes the names of all of the OpenAPI security scheme converters.
- Loading branch information
Showing
15 changed files
with
202 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
smithy-model/src/test/java/software/amazon/smithy/model/traits/HttpApiKeyAuthTraitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2019 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.traits; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.Optional; | ||
import org.junit.jupiter.api.Test; | ||
import software.amazon.smithy.model.node.Node; | ||
import software.amazon.smithy.model.node.ObjectNode; | ||
import software.amazon.smithy.model.shapes.ShapeId; | ||
|
||
public class HttpApiKeyAuthTraitTest { | ||
@Test | ||
public void loadsTraitWithHeader() { | ||
TraitFactory provider = TraitFactory.createServiceFactory(); | ||
ObjectNode node = Node.objectNode() | ||
.withMember("name", "X-Foo") | ||
.withMember("in", "header"); | ||
Optional<Trait> trait = provider.createTrait( | ||
HttpApiKeyAuthTrait.ID, ShapeId.from("ns.qux#foo"), node); | ||
assertTrue(trait.isPresent()); | ||
assertThat(trait.get(), instanceOf(HttpApiKeyAuthTrait.class)); | ||
HttpApiKeyAuthTrait auth = (HttpApiKeyAuthTrait) trait.get(); | ||
|
||
assertThat(auth.getName(), equalTo("X-Foo")); | ||
assertThat(auth.getIn(), equalTo(HttpApiKeyAuthTrait.Location.HEADER)); | ||
assertThat(auth.toNode(), equalTo(node)); | ||
assertThat(auth.toBuilder().build(), equalTo(auth)); | ||
} | ||
|
||
@Test | ||
public void loadsTraitWithQuery() { | ||
TraitFactory provider = TraitFactory.createServiceFactory(); | ||
ObjectNode node = Node.objectNode() | ||
.withMember("name", "blerg") | ||
.withMember("in", "query"); | ||
Optional<Trait> trait = provider.createTrait( | ||
HttpApiKeyAuthTrait.ID, ShapeId.from("ns.qux#foo"), node); | ||
assertTrue(trait.isPresent()); | ||
assertThat(trait.get(), instanceOf(HttpApiKeyAuthTrait.class)); | ||
HttpApiKeyAuthTrait auth = (HttpApiKeyAuthTrait) trait.get(); | ||
|
||
assertThat(auth.getName(), equalTo("blerg")); | ||
assertThat(auth.getIn(), equalTo(HttpApiKeyAuthTrait.Location.QUERY)); | ||
assertThat(auth.toNode(), equalTo(node)); | ||
assertThat(auth.toBuilder().build(), equalTo(auth)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
.../java/software/amazon/smithy/openapi/fromsmithy/security/HttpApiKeyAuthConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package software.amazon.smithy.openapi.fromsmithy.security; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.node.Node; | ||
import software.amazon.smithy.model.shapes.ShapeId; | ||
import software.amazon.smithy.openapi.fromsmithy.OpenApiConverter; | ||
import software.amazon.smithy.openapi.model.OpenApi; | ||
import software.amazon.smithy.utils.IoUtils; | ||
|
||
public class HttpApiKeyAuthConverterTest { | ||
@Test | ||
public void addsCustomApiKeyAuth() { | ||
Model model = Model.assembler() | ||
.addImport(getClass().getResource("http-api-key-security.json")) | ||
.discoverModels() | ||
.assemble() | ||
.unwrap(); | ||
OpenApi result = OpenApiConverter.create().convert(model, ShapeId.from("smithy.example#Service")); | ||
Node expectedNode = Node.parse(IoUtils.toUtf8String( | ||
getClass().getResourceAsStream("http-api-key-security.openapi.json"))); | ||
|
||
Node.assertEquals(result, expectedNode); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...t/resources/software/amazon/smithy/openapi/fromsmithy/security/http-api-key-security.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"smithy": "0.5.0", | ||
"shapes": { | ||
"smithy.example#Service": { | ||
"type": "service", | ||
"version": "2006-03-01", | ||
"operations": [ | ||
{ | ||
"target": "smithy.example#Operation1" | ||
} | ||
], | ||
"traits": { | ||
"aws.protocols#restJson1": true, | ||
"smithy.api#httpApiKeyAuth": { | ||
"name": "X-Api-Key", | ||
"in": "header" | ||
} | ||
} | ||
}, | ||
"smithy.example#Operation1": { | ||
"type": "operation", | ||
"traits": { | ||
"smithy.api#http": { | ||
"uri": "/", | ||
"method": "GET" | ||
} | ||
} | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...ces/software/amazon/smithy/openapi/fromsmithy/security/http-api-key-security.openapi.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"openapi": "3.0.2", | ||
"info": { | ||
"title": "Service", | ||
"version": "2006-03-01" | ||
}, | ||
"paths": { | ||
"/": { | ||
"get": { | ||
"operationId": "Operation1", | ||
"responses": { | ||
"200": { | ||
"description": "Operation1 response" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"components": { | ||
"securitySchemes": { | ||
"smithy.api#httpApiKeyAuth": { | ||
"type": "apiKey", | ||
"name": "X-Api-Key", | ||
"in": "header" | ||
} | ||
} | ||
}, | ||
"security": [ | ||
{ | ||
"smithy.api#httpApiKeyAuth": [ ] | ||
} | ||
] | ||
} |