-
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.
Implement a trait to support adding specification extensions to OpenAPI
Additionally add a JsonSchemaMapperV2 interface rather than breaking original.
- Loading branch information
Showing
27 changed files
with
1,064 additions
and
9 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
58 changes: 58 additions & 0 deletions
58
...y-jsonschema/src/main/java/software/amazon/smithy/jsonschema/JsonSchemaMapperContext.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,58 @@ | ||
/* | ||
* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
|
||
package software.amazon.smithy.jsonschema; | ||
|
||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.shapes.Shape; | ||
|
||
/** | ||
* Context for a JSON schema mapping. | ||
* | ||
* @param <T> Type of Smithy {@link Shape} being mapped. | ||
*/ | ||
public class JsonSchemaMapperContext<T extends Shape> { | ||
private final Model model; | ||
private final T shape; | ||
private final JsonSchemaConfig config; | ||
|
||
JsonSchemaMapperContext( | ||
Model model, | ||
T shape, | ||
JsonSchemaConfig config | ||
) { | ||
this.model = model; | ||
this.shape = shape; | ||
this.config = config; | ||
} | ||
|
||
/** | ||
* Gets the Smithy model being converted. | ||
* | ||
* @return Returns the Smithy model. | ||
*/ | ||
public Model getModel() { | ||
return model; | ||
} | ||
|
||
/** | ||
* Gets the Smithy shape being mapped. | ||
* | ||
* @return Returns the Smithy shape. | ||
*/ | ||
public T getShape() { | ||
return shape; | ||
} | ||
|
||
/** | ||
* Gets the JSON schema configuration object. | ||
* | ||
* @return Returns the JSON schema config object. | ||
*/ | ||
public JsonSchemaConfig getConfig() { | ||
return config; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
smithy-jsonschema/src/main/java/software/amazon/smithy/jsonschema/JsonSchemaMapperV2.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,23 @@ | ||
/* | ||
* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.jsonschema; | ||
|
||
import software.amazon.smithy.model.shapes.Shape; | ||
|
||
public interface JsonSchemaMapperV2 extends JsonSchemaMapper { | ||
default Schema.Builder updateSchema(Shape shape, Schema.Builder schemaBuilder, JsonSchemaConfig config) { | ||
return schemaBuilder; | ||
} | ||
|
||
/** | ||
* Updates a schema builder. | ||
* | ||
* @param context Context of this schema mapping. | ||
* @param schemaBuilder Schema builder to update. | ||
* @return Returns an updated schema builder. | ||
*/ | ||
Schema.Builder updateSchema(JsonSchemaMapperContext<? extends Shape> context, Schema.Builder schemaBuilder); | ||
} |
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,15 @@ | ||
/* | ||
* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
description = "This module provides Smithy traits that are used in converting a Smithy model to OpenAPI." | ||
|
||
ext { | ||
displayName = "Smithy :: OpenAPI Traits" | ||
moduleName = "software.amazon.smithy.openapi.traits" | ||
} | ||
|
||
dependencies { | ||
api project(":smithy-model") | ||
} |
103 changes: 103 additions & 0 deletions
103
...aits/src/main/java/software/amazon/smithy/openapi/traits/SpecificationExtensionTrait.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,103 @@ | ||
/* | ||
* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.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.ToSmithyBuilder; | ||
|
||
/** | ||
* <code>smithy.openapi#specificationExtension</code> - Indicates a trait shape should be converted into an <a href="https://spec.openapis.org/oas/v3.1.0#specification-extensions">OpenAPI specification extension</a>. | ||
*/ | ||
public final class SpecificationExtensionTrait extends AbstractTrait | ||
implements ToSmithyBuilder<SpecificationExtensionTrait> { | ||
public static final ShapeId ID = ShapeId.from("smithy.openapi#specificationExtension"); | ||
|
||
private static final String AS_MEMBER_NAME = "as"; | ||
|
||
private final String as; | ||
|
||
private SpecificationExtensionTrait(SpecificationExtensionTrait.Builder builder) { | ||
super(ID, builder.getSourceLocation()); | ||
this.as = builder.as; | ||
} | ||
|
||
public static final class Provider extends AbstractTrait.Provider { | ||
public Provider() { | ||
super(ID); | ||
} | ||
|
||
@Override | ||
public Trait createTrait(ShapeId target, Node value) { | ||
ObjectNode node = value.expectObjectNode(); | ||
SpecificationExtensionTrait.Builder builder = builder().sourceLocation(value); | ||
node.getStringMember(AS_MEMBER_NAME, builder::as); | ||
SpecificationExtensionTrait trait = builder.build(); | ||
trait.setNodeCache(value); | ||
return trait; | ||
} | ||
} | ||
|
||
/** | ||
* Gets the extension name for a given trait shape. | ||
* Either an explicitly configured extension name, or a default transformation of the shape ID. | ||
* | ||
* @param traitShapeId Trait shape to get the extension name. | ||
* @return Extension name for the given trait shape. | ||
*/ | ||
public String extensionNameFor(ShapeId traitShapeId) { | ||
return as != null | ||
? as | ||
: "x-" + traitShapeId.toString().replaceAll("[.#]", "-"); | ||
} | ||
|
||
public static SpecificationExtensionTrait.Builder builder() { | ||
return new SpecificationExtensionTrait.Builder(); | ||
} | ||
|
||
@Override | ||
protected Node createNode() { | ||
return Node.objectNodeBuilder() | ||
.sourceLocation(getSourceLocation()) | ||
.withMember(AS_MEMBER_NAME, this.as) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public SpecificationExtensionTrait.Builder toBuilder() { | ||
return builder() | ||
.sourceLocation(getSourceLocation()) | ||
.as(this.as); | ||
} | ||
|
||
public static final class Builder | ||
extends AbstractTraitBuilder<SpecificationExtensionTrait, SpecificationExtensionTrait.Builder> { | ||
private String as; | ||
|
||
private Builder() { | ||
} | ||
|
||
@Override | ||
public SpecificationExtensionTrait build() { | ||
return new SpecificationExtensionTrait(this); | ||
} | ||
|
||
/** | ||
* Set the explicit name for the target specification extension. | ||
* | ||
* @param as Explicit name for the target specification extension, or null. | ||
* @return This builder instance. | ||
*/ | ||
public SpecificationExtensionTrait.Builder as(String as) { | ||
this.as = as; | ||
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 @@ | ||
software.amazon.smithy.openapi.traits.SpecificationExtensionTrait$Provider |
1 change: 1 addition & 0 deletions
1
smithy-openapi-traits/src/main/resources/META-INF/smithy/manifest
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 @@ | ||
smithy.openapi.smithy |
Oops, something went wrong.