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

Update open api json schema pojos #364

Merged
merged 3 commits into from
Apr 9, 2020
Merged
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
301 changes: 215 additions & 86 deletions docs/source/guides/converting-to-openapi.rst

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.aws.apigateway.openapi;

/**
* API Gateway OpenAPI configuration.
*/
public final class ApiGatewayConfig {

private boolean disableCloudFormationSubstitution;

public boolean getDisableCloudFormationSubstitution() {
return disableCloudFormationSubstitution;
}

/**
* Disables CloudFormation substitutions of specific paths when they contain
* ${} placeholders. When found, these are expanded into CloudFormation Fn::Sub
* intrinsic functions.
*
* @param disableCloudFormationSubstitution Set to true to disable intrinsics.
*/
public void setDisableCloudFormationSubstitution(boolean disableCloudFormationSubstitution) {
this.disableCloudFormationSubstitution = disableCloudFormationSubstitution;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.node.StringNode;
import software.amazon.smithy.model.traits.Trait;
import software.amazon.smithy.openapi.OpenApiConstants;
import software.amazon.smithy.openapi.OpenApiConfig;
import software.amazon.smithy.openapi.fromsmithy.Context;
import software.amazon.smithy.openapi.fromsmithy.OpenApiMapper;
import software.amazon.smithy.openapi.model.OpenApi;
@@ -51,7 +51,7 @@ final class CloudFormationSubstitution implements OpenApiMapper {
* commonly extracted out of CloudFormation. This list may need to be updated over
* time as new features are added. Note that this list only expands to simple
* Fn::Sub. Anything more complex needs to be handled through JSON substitutions
* via {@link OpenApiConstants#SUBSTITUTIONS} as can anything that does not appear
* via {@link OpenApiConfig#setSubstitutions} as can anything that does not appear
* in this list.
*/
private static final List<String> PATHS = Arrays.asList(
@@ -69,7 +69,7 @@ public byte getOrder() {

@Override
public ObjectNode updateNode(Context<? extends Trait> context, OpenApi openapi, ObjectNode node) {
if (!context.getConfig().getBooleanMemberOrDefault(ApiGatewayConstants.DISABLE_CLOUDFORMATION_SUBSTITUTION)) {
if (!context.getConfig().getExtensions(ApiGatewayConfig.class).getDisableCloudFormationSubstitution()) {
return node.accept(new CloudFormationFnSubInjector(PATHS)).expectObjectNode();
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@
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.openapi.OpenApiConfig;
import software.amazon.smithy.openapi.fromsmithy.OpenApiConverter;
import software.amazon.smithy.utils.IoUtils;

@@ -55,9 +56,14 @@ public void pluginCanBeDisabled() {
IoUtils.readUtf8File(getClass().getResource("substitution-not-performed.json").getPath()))
.expectObjectNode();

OpenApiConfig config = new OpenApiConfig();
ApiGatewayConfig apiGatewayConfig = new ApiGatewayConfig();
apiGatewayConfig.setDisableCloudFormationSubstitution(true);
config.putExtensions(apiGatewayConfig);

ObjectNode actual = OpenApiConverter.create()
.classLoader(getClass().getClassLoader())
.putSetting(ApiGatewayConstants.DISABLE_CLOUDFORMATION_SUBSTITUTION, true)
.config(config)
.convertToNode(model, ShapeId.from("example.smithy#MyService"));

Node.assertEquals(expected, actual);
Original file line number Diff line number Diff line change
@@ -17,14 +17,12 @@

import java.util.regex.Pattern;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.shapes.CollectionShape;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.shapes.SimpleShape;
import software.amazon.smithy.model.traits.EnumTrait;
import software.amazon.smithy.utils.StringUtils;

/**
* This ref strategy converts Smithy shapes into the following:
@@ -55,27 +53,22 @@
*/
final class DefaultRefStrategy implements RefStrategy {

private static final Pattern SPLIT_PATTERN = Pattern.compile("\\.");
private static final Pattern NON_ALPHA_NUMERIC = Pattern.compile("[^A-Za-z0-9]");

private final Model model;
private final boolean alphanumericOnly;
private final boolean keepNamespaces;
private final String rootPointer;
private final PropertyNamingStrategy propertyNamingStrategy;
private final ObjectNode config;
private final JsonSchemaConfig config;

DefaultRefStrategy(Model model, ObjectNode config, PropertyNamingStrategy propertyNamingStrategy) {
DefaultRefStrategy(Model model, JsonSchemaConfig config, PropertyNamingStrategy propertyNamingStrategy) {
this.model = model;
this.propertyNamingStrategy = propertyNamingStrategy;
this.config = config;
rootPointer = computePointer(config);
alphanumericOnly = config.getBooleanMemberOrDefault(JsonSchemaConstants.ALPHANUMERIC_ONLY_REFS);
keepNamespaces = config.getBooleanMemberOrDefault(JsonSchemaConstants.KEEP_NAMESPACES);
}

private static String computePointer(ObjectNode config) {
String pointer = config.getStringMemberOrDefault(JsonSchemaConstants.DEFINITION_POINTER, DEFAULT_POINTER);
private static String computePointer(JsonSchemaConfig config) {
String pointer = config.getDefinitionPointer();
if (!pointer.endsWith("/")) {
pointer += "/";
}
@@ -89,10 +82,7 @@ public String toPointer(ShapeId id) {
return createMemberPointer(member);
}

StringBuilder builder = new StringBuilder();
appendNamespace(builder, id);
builder.append(id.getName());
return rootPointer + stripNonAlphaNumericCharsIfNecessary(builder.toString());
return rootPointer + stripNonAlphaNumericCharsIfNecessary(id.getName());
}

private String createMemberPointer(MemberShape member) {
@@ -158,18 +148,8 @@ public boolean isInlined(Shape shape) {
return shape instanceof SimpleShape;
}

private void appendNamespace(StringBuilder builder, ShapeId id) {
// Append each namespace part, capitalizing each segment.
// For example, "smithy.example" becomes "SmithyExample".
if (keepNamespaces) {
for (String part : SPLIT_PATTERN.split(id.getNamespace())) {
builder.append(StringUtils.capitalize(part));
}
}
}

private String stripNonAlphaNumericCharsIfNecessary(String result) {
return alphanumericOnly
return config.getAlphanumericOnlyRefs()
? NON_ALPHA_NUMERIC.matcher(result).replaceAll("")
: result;
}
Original file line number Diff line number Diff line change
@@ -15,12 +15,11 @@

package software.amazon.smithy.jsonschema;

import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.shapes.Shape;

/**
* Removes keywords from a Schema builder that have been disabled using
* the settings object {@code disable.*} flags.
* {@link JsonSchemaConfig#setDisableFeatures}.
*/
final class DisableMapper implements JsonSchemaMapper {
@Override
@@ -29,9 +28,9 @@ public byte getOrder() {
}

@Override
public Schema.Builder updateSchema(Shape shape, Schema.Builder schema, ObjectNode config) {
for (String key : config.getMembersByPrefix("disable.").keySet()) {
schema.disableProperty(key);
public Schema.Builder updateSchema(Shape shape, Schema.Builder schema, JsonSchemaConfig config) {
for (String feature : config.getDisableFeatures()) {
schema.disableProperty(feature);
}

return schema;
Loading