Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ private SmithyIntermediateModelPostprocessor(List<CodegenCustomizationProcessor>

public static SmithyIntermediateModelPostprocessor create(CustomizationConfig config) {
List<CodegenCustomizationProcessor> processors = new ArrayList<>();
processors.add(new MetadataModifiersProcessor(config.getCustomServiceMetadata()));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there might be a small gap here. MetadataModifiersProcessor has two halves. postprocess applies contentType (now wired up). preprocess applies customServiceMetadata.protocol by overwriting ServiceMetadata.protocols — and this chain only ever calls postprocess, so on the Smithy path protocol is silently ignored. The only user in our services of protocol is Kinesis - but could potentially impact custom internal services....

I'd lean towards a fail fast check: throw in SmithyIntermediateModelPostprocessor.create when customServiceMetadata.protocol is non-null on the Smithy path (with a message pointing at addTraits + protocol traits which are the correct (and now supported) smithy way to handle this).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this one, I have another PR #7442 to apply this preprocessing just for kinesis. And the old CBOR protocol has no Smithy trait..

processors.add(new RemoveExceptionMessagePropertyProcessor());
processors.add(new UseLegacyEventGenerationSchemeProcessor());
processors.add(new EventStreamSharedEventProcessor(config.getDuplicateAndRenameSharedEvents()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ protected ShapeModel generateShapeModel(String javaClassName,
if (smithyShape instanceof EnumShape) {
for (MemberShape enumMember : ((EnumShape) smithyShape).members()) {
String value = enumMember.expectTrait(EnumValueTrait.class).expectStringValue();
shapeModel.addEnum(new EnumModel(namingStrategy.getEnumValueName(value), value));
String name = SmithyCodegenTraits.enumConstantName(enumMember, value, namingStrategy);
shapeModel.addEnum(new EnumModel(name, value));
}
}

Expand Down Expand Up @@ -450,6 +451,10 @@ private MemberModel generateMemberModel(MemberShape member,
});

memberModel.setEndpointDiscoveryId(member.hasTrait(ClientEndpointDiscoveryIdTrait.class));
SmithyCodegenTraits.applySynthetic(memberModel, targetShape);
SmithyCodegenTraits.applyAlternateBeanPropertyName(memberModel, member);
SmithyCodegenTraits.applyIgnoreDataTypeConversionFailures(memberModel, member);
SmithyCodegenTraits.applyDeprecatedName(memberModel, member, namingStrategy, parentCtx, shapeCtx);

// Only string enums map to a Java enum. C2J has no int-enum concept: an intEnum is written
// as a plain integer shape, so a member targeting one is a plain Integer with no enumType.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 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.awssdk.codegen.smithy;

import software.amazon.awssdk.codegen.model.intermediate.MemberModel;
import software.amazon.awssdk.codegen.naming.NamingStrategy;
import software.amazon.awssdk.codegen.naming.ShapeInfo;
import software.amazon.awssdk.codegen.smithy.traits.AlternateBeanPropertyNameTrait;
import software.amazon.awssdk.codegen.smithy.traits.DeprecatedNameTrait;
import software.amazon.awssdk.codegen.smithy.traits.EnumConstantNameTrait;
import software.amazon.awssdk.codegen.smithy.traits.IgnoreDataTypeConversionFailuresTrait;
import software.amazon.awssdk.codegen.smithy.traits.SyntheticTrait;
import software.amazon.awssdk.utils.StringUtils;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.Shape;

/**
* Applies the codegen traits that carry Java-only customizations (see {@code software.amazon.awssdk.codegen.smithy
* .traits}) to the intermediate model, setting the same fields C2J's {@code AddShapes} sets.
*/
final class SmithyCodegenTraits {

private SmithyCodegenTraits() {
}

/**
* Read from the member's target shape, as C2J marks the added SDK shape rather than the member.
*/
static void applySynthetic(MemberModel memberModel, Shape targetShape) {
memberModel.setSynthetic(targetShape.hasTrait(SyntheticTrait.class));
}

static void applyAlternateBeanPropertyName(MemberModel memberModel, MemberShape member) {
member.getTrait(AlternateBeanPropertyNameTrait.class).ifPresent(
t -> memberModel.setAdditionalBeanStyleSetterName(String.format("set%s", t.getValue())));
}

static void applyIgnoreDataTypeConversionFailures(MemberModel memberModel, MemberShape member) {
if (member.hasTrait(IgnoreDataTypeConversionFailuresTrait.class)) {
memberModel.ignoreDataTypeConversionFailures(true);
}
}

static void applyDeprecatedName(MemberModel memberModel, MemberShape member, NamingStrategy namingStrategy,
ShapeInfo parentCtx, ShapeInfo shapeCtx) {
String deprecatedName = member.getTrait(DeprecatedNameTrait.class)
.map(DeprecatedNameTrait::getValue)
.orElse(null);
if (StringUtils.isBlank(deprecatedName)) {
return;
}
memberModel.setDeprecatedName(deprecatedName);
memberModel.setDeprecatedFluentGetterMethodName(
namingStrategy.getFluentGetterMethodName(deprecatedName, parentCtx, shapeCtx));
memberModel.setDeprecatedFluentSetterMethodName(
namingStrategy.getFluentSetterMethodName(deprecatedName, parentCtx, shapeCtx));
memberModel.setDeprecatedBeanStyleSetterMethodName(
namingStrategy.getBeanStyleSetterMethodName(deprecatedName, parentCtx, shapeCtx));
}

/**
* Read from an enum value, not a structure member. Falls back to the name derived from the value.
*/
static String enumConstantName(MemberShape enumMember, String value, NamingStrategy namingStrategy) {
return enumMember.getTrait(EnumConstantNameTrait.class)
.map(EnumConstantNameTrait::getValue)
.orElseGet(() -> namingStrategy.getEnumValueName(value));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 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.awssdk.codegen.smithy.traits;

import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.StringTrait;

/**
* {@code software.amazon.awssdk.codegen#alternateBeanPropertyName}: the generated class gets an extra bean-style
* setter named {@code set} followed by this value. Replaces the {@code customization.config} setting of the same name.
*/
@SdkInternalApi
public final class AlternateBeanPropertyNameTrait extends StringTrait {

public static final ShapeId ID = ShapeId.from("software.amazon.awssdk.codegen#alternateBeanPropertyName");

public AlternateBeanPropertyNameTrait(String value, SourceLocation sourceLocation) {
super(ID, value, sourceLocation);
}

public AlternateBeanPropertyNameTrait(String value) {
this(value, SourceLocation.NONE);
}

/**
* Registered in {@code META-INF/services} so Smithy builds this class when it loads the trait.
*/
public static final class Provider extends StringTrait.Provider<AlternateBeanPropertyNameTrait> {
public Provider() {
super(ID, AlternateBeanPropertyNameTrait::new);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 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.awssdk.codegen.smithy.traits;

import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.StringTrait;

/**
* {@code software.amazon.awssdk.codegen#deprecatedName}: the member's name before a rename. The generated class keeps
* deprecated accessors under this name. Replaces {@code existingNameDeprecated} in {@code customization.config}.
*/
@SdkInternalApi
public final class DeprecatedNameTrait extends StringTrait {

public static final ShapeId ID = ShapeId.from("software.amazon.awssdk.codegen#deprecatedName");

public DeprecatedNameTrait(String value, SourceLocation sourceLocation) {
super(ID, value, sourceLocation);
}

public DeprecatedNameTrait(String value) {
this(value, SourceLocation.NONE);
}

/**
* Registered in {@code META-INF/services} so Smithy builds this class when it loads the trait.
*/
public static final class Provider extends StringTrait.Provider<DeprecatedNameTrait> {
public Provider() {
super(ID, DeprecatedNameTrait::new);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 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.awssdk.codegen.smithy.traits;

import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.StringTrait;

/**
* {@code software.amazon.awssdk.codegen#enumConstantName}: the Java enum constant name for an enum member, used instead
* of the name derived from its value. Replaces {@code emitEnumName} in {@code customization.config}.
*/
@SdkInternalApi
public final class EnumConstantNameTrait extends StringTrait {

public static final ShapeId ID = ShapeId.from("software.amazon.awssdk.codegen#enumConstantName");

public EnumConstantNameTrait(String value, SourceLocation sourceLocation) {
super(ID, value, sourceLocation);
}

public EnumConstantNameTrait(String value) {
this(value, SourceLocation.NONE);
}

/**
* Registered in {@code META-INF/services} so Smithy builds this class when it loads the trait.
*/
public static final class Provider extends StringTrait.Provider<EnumConstantNameTrait> {
public Provider() {
super(ID, EnumConstantNameTrait::new);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 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.awssdk.codegen.smithy.traits;

import software.amazon.awssdk.annotations.SdkInternalApi;
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.AnnotationTrait;

/**
* {@code software.amazon.awssdk.codegen#ignoreDataTypeConversionFailures}: the unmarshaller ignores a value that cannot
* be converted to the member's type. Replaces the {@code customization.config} setting of the same name.
*/
@SdkInternalApi
public final class IgnoreDataTypeConversionFailuresTrait extends AnnotationTrait {

public static final ShapeId ID = ShapeId.from("software.amazon.awssdk.codegen#ignoreDataTypeConversionFailures");

public IgnoreDataTypeConversionFailuresTrait(ObjectNode node) {
super(ID, node);
}

public IgnoreDataTypeConversionFailuresTrait() {
this(Node.objectNode());
}

/**
* Registered in {@code META-INF/services} so Smithy builds this class when it loads the trait.
*/
public static final class Provider extends AnnotationTrait.Provider<IgnoreDataTypeConversionFailuresTrait> {
public Provider() {
super(ID, IgnoreDataTypeConversionFailuresTrait::new);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 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.awssdk.codegen.smithy.traits;

import software.amazon.awssdk.annotations.SdkInternalApi;
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.AnnotationTrait;

/**
* {@code software.amazon.awssdk.codegen#synthetic}: a shape added for SDK use only. Members that target it are not
* marshalled. Marks the shapes that {@code customSdkShapes} in {@code customization.config} adds.
*/
@SdkInternalApi
public final class SyntheticTrait extends AnnotationTrait {

public static final ShapeId ID = ShapeId.from("software.amazon.awssdk.codegen#synthetic");

public SyntheticTrait(ObjectNode node) {
super(ID, node);
}

public SyntheticTrait() {
this(Node.objectNode());
}

/**
* Registered in {@code META-INF/services} so Smithy builds this class when it loads the trait.
*/
public static final class Provider extends AnnotationTrait.Provider<SyntheticTrait> {
public Provider() {
super(ID, SyntheticTrait::new);
}
}
}
Loading
Loading