-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Define trait and extension for adding
x-
properties to openapi outp…
…ut (#73) * Define a trait and extension for adding `x-` properties to openapi output Signed-off-by: Thomas Farr <tsfarr@amazon.com> * Add small documentation example Signed-off-by: Thomas Farr <tsfarr@amazon.com> --------- Signed-off-by: Thomas Farr <tsfarr@amazon.com>
- Loading branch information
Showing
15 changed files
with
228 additions
and
4 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
plugins { | ||
java | ||
`java-library` | ||
id("com.diffplug.spotless").version("6.11.0") | ||
} | ||
|
||
group = "org.opensearch.smithy" | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation("software.amazon.smithy:smithy-openapi:1.26.0") | ||
} | ||
|
||
spotless { | ||
kotlinGradle { | ||
target("**/*.kts", "**/*.java", "**/*.smithy") | ||
|
||
indentWithSpaces() | ||
endWithNewline() | ||
trimTrailingWhitespace() | ||
} | ||
} |
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 @@ | ||
rootProject.name = "openapi-traits" |
21 changes: 21 additions & 0 deletions
21
...its/src/main/java/org/opensearch/smithy/openapi/extensions/VendorExtensionsExtension.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,21 @@ | ||
package org.opensearch.smithy.openapi.extensions; | ||
|
||
import org.opensearch.smithy.openapi.extensions.mappers.VendorExtensionsJsonSchemaMapper; | ||
import org.opensearch.smithy.openapi.extensions.mappers.VendorExtensionsOpenApiMapper; | ||
import software.amazon.smithy.jsonschema.JsonSchemaMapper; | ||
import software.amazon.smithy.openapi.fromsmithy.OpenApiMapper; | ||
import software.amazon.smithy.openapi.fromsmithy.Smithy2OpenApiExtension; | ||
|
||
import java.util.List; | ||
|
||
public class VendorExtensionsExtension implements Smithy2OpenApiExtension { | ||
@Override | ||
public List<JsonSchemaMapper> getJsonSchemaMappers() { | ||
return List.of(new VendorExtensionsJsonSchemaMapper()); | ||
} | ||
|
||
@Override | ||
public List<OpenApiMapper> getOpenApiMappers() { | ||
return List.of(new VendorExtensionsOpenApiMapper()); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...va/org/opensearch/smithy/openapi/extensions/mappers/VendorExtensionsJsonSchemaMapper.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,19 @@ | ||
package org.opensearch.smithy.openapi.extensions.mappers; | ||
|
||
import org.opensearch.smithy.openapi.traits.VendorExtensionsTrait; | ||
import software.amazon.smithy.jsonschema.JsonSchemaConfig; | ||
import software.amazon.smithy.jsonschema.JsonSchemaMapper; | ||
import software.amazon.smithy.jsonschema.Schema; | ||
import software.amazon.smithy.model.shapes.Shape; | ||
|
||
public class VendorExtensionsJsonSchemaMapper implements JsonSchemaMapper { | ||
@Override | ||
public Schema.Builder updateSchema(Shape shape, Schema.Builder schemaBuilder, JsonSchemaConfig config) { | ||
shape.getTrait(VendorExtensionsTrait.class) | ||
.ifPresent(trait -> trait.getNode() | ||
.getMembers() | ||
.forEach((k, v) -> schemaBuilder.putExtension(k.getValue(), v))); | ||
|
||
return schemaBuilder; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
.../java/org/opensearch/smithy/openapi/extensions/mappers/VendorExtensionsOpenApiMapper.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 org.opensearch.smithy.openapi.extensions.mappers; | ||
|
||
import org.opensearch.smithy.openapi.traits.VendorExtensionsTrait; | ||
import software.amazon.smithy.model.shapes.OperationShape; | ||
import software.amazon.smithy.model.traits.Trait; | ||
import software.amazon.smithy.openapi.fromsmithy.Context; | ||
import software.amazon.smithy.openapi.fromsmithy.OpenApiMapper; | ||
import software.amazon.smithy.openapi.model.OperationObject; | ||
|
||
public class VendorExtensionsOpenApiMapper implements OpenApiMapper { | ||
@Override | ||
public OperationObject updateOperation(Context<? extends Trait> context, OperationShape shape, OperationObject operation, String httpMethodName, String path) { | ||
return shape.getTrait(VendorExtensionsTrait.class) | ||
.map(trait -> { | ||
OperationObject.Builder builder = operation.toBuilder(); | ||
|
||
trait.getNode() | ||
.getMembers() | ||
.forEach((k, v) -> builder.putExtension(k.getValue(), v)); | ||
|
||
return builder.build(); | ||
}) | ||
.orElse(operation); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
openapi-traits/src/main/java/org/opensearch/smithy/openapi/traits/VendorExtensionsTrait.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,75 @@ | ||
package org.opensearch.smithy.openapi.traits; | ||
|
||
import software.amazon.smithy.model.node.Node; | ||
import software.amazon.smithy.model.node.ObjectNode; | ||
import software.amazon.smithy.model.shapes.ShapeId; | ||
import software.amazon.smithy.model.traits.AbstractTrait; | ||
import software.amazon.smithy.model.traits.AbstractTraitBuilder; | ||
import software.amazon.smithy.model.traits.Trait; | ||
import software.amazon.smithy.utils.SmithyBuilder; | ||
import software.amazon.smithy.utils.ToSmithyBuilder; | ||
|
||
public final class VendorExtensionsTrait extends AbstractTrait implements ToSmithyBuilder<VendorExtensionsTrait> { | ||
public static final ShapeId ID = ShapeId.from("opensearch.openapi#vendorExtensions"); | ||
|
||
private final ObjectNode node; | ||
|
||
private VendorExtensionsTrait(Builder builder) { | ||
super(ID, builder.getSourceLocation()); | ||
this.node = SmithyBuilder.requiredState("node", builder.node); | ||
} | ||
|
||
public static final class Provider extends AbstractTrait.Provider { | ||
public Provider() { | ||
super(ID); | ||
} | ||
|
||
@Override | ||
public Trait createTrait(ShapeId target, Node value) { | ||
ObjectNode node = value.expectObjectNode(); | ||
VendorExtensionsTrait trait = builder().sourceLocation(value).node(node).build(); | ||
trait.setNodeCache(value); | ||
return trait; | ||
} | ||
} | ||
|
||
public ObjectNode getNode() { | ||
return this.node; | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
@Override | ||
protected Node createNode() { | ||
return Node.objectNodeBuilder() | ||
.sourceLocation(getSourceLocation()) | ||
.merge(node) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public Builder toBuilder() { | ||
return builder() | ||
.sourceLocation(getSourceLocation()) | ||
.node(node); | ||
} | ||
|
||
public static final class Builder extends AbstractTraitBuilder<VendorExtensionsTrait, Builder> { | ||
private ObjectNode node; | ||
|
||
private Builder() { | ||
} | ||
|
||
@Override | ||
public VendorExtensionsTrait build() { | ||
return new VendorExtensionsTrait(this); | ||
} | ||
|
||
public Builder node(ObjectNode node) { | ||
this.node = node; | ||
return this; | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...its/src/main/resources/META-INF/services/software.amazon.smithy.model.traits.TraitService
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 @@ | ||
org.opensearch.smithy.openapi.traits.VendorExtensionsTrait$Provider |
1 change: 1 addition & 0 deletions
1
...urces/META-INF/services/software.amazon.smithy.openapi.fromsmithy.Smithy2OpenApiExtension
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 @@ | ||
org.opensearch.smithy.openapi.extensions.VendorExtensionsExtension |
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 @@ | ||
opensearch.openapi.smithy |
12 changes: 12 additions & 0 deletions
12
openapi-traits/src/main/resources/META-INF/smithy/opensearch.openapi.smithy
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,12 @@ | ||
$version: "2" | ||
|
||
namespace opensearch.openapi | ||
|
||
@trait( | ||
selector: ":is(simpleType, list, map, structure, union, operation, member)" | ||
) | ||
map vendorExtensions { | ||
@pattern("^x-.+$") | ||
key: String | ||
value: Document | ||
} |
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,3 @@ | ||
rootProject.name = "opensearch-api-specification" | ||
|
||
includeBuild("openapi-traits") |
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 |
---|---|---|
@@ -1,9 +1,15 @@ | ||
{ | ||
"version": "1.0", | ||
"plugins": { | ||
"openapi": { | ||
"service": "OpenSearch#OpenSearch", | ||
"protocol": "aws.protocols#restJson1" | ||
"projections": { | ||
"full": { | ||
"plugins": { | ||
"openapi": { | ||
"service": "OpenSearch#OpenSearch", | ||
"protocol": "aws.protocols#restJson1", | ||
"tags": true, | ||
"useIntegerType": true | ||
} | ||
} | ||
} | ||
} | ||
} |