Skip to content

Commit 32aeacc

Browse files
committed
Adding valitation errors for when both underscore configs are supplied
1 parent def4747 commit 32aeacc

File tree

4 files changed

+70
-5
lines changed

4 files changed

+70
-5
lines changed

codegen/src/main/java/software/amazon/awssdk/codegen/CodeGenerator.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import java.io.IOException;
2121
import java.io.PrintWriter;
2222
import java.util.ArrayList;
23-
import java.util.Collections;
23+
import java.util.Arrays;
2424
import java.util.List;
2525
import java.util.concurrent.ForkJoinTask;
2626
import software.amazon.awssdk.codegen.emitters.GeneratorTask;
@@ -29,6 +29,7 @@
2929
import software.amazon.awssdk.codegen.internal.Jackson;
3030
import software.amazon.awssdk.codegen.internal.Utils;
3131
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
32+
import software.amazon.awssdk.codegen.validation.CustomizationConfigValidator;
3233
import software.amazon.awssdk.codegen.validation.ModelInvalidException;
3334
import software.amazon.awssdk.codegen.validation.ModelValidationContext;
3435
import software.amazon.awssdk.codegen.validation.ModelValidationReport;
@@ -41,8 +42,9 @@ public class CodeGenerator {
4142
private static final Logger log = Logger.loggerFor(CodeGenerator.class);
4243
private static final String MODEL_DIR_NAME = "models";
4344

44-
private static final List<ModelValidator> DEFAULT_MODEL_VALIDATORS = Collections.singletonList(
45-
new SharedModelsValidator()
45+
private static final List<ModelValidator> DEFAULT_MODEL_VALIDATORS = Arrays.asList(
46+
new SharedModelsValidator(),
47+
new CustomizationConfigValidator()
4648
);
4749

4850
private final C2jModels c2jModels;

codegen/src/main/java/software/amazon/awssdk/codegen/naming/DefaultNamingStrategy.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,13 +499,12 @@ private void validateCustomerVisibleName(String name, String location) {
499499
}
500500

501501
if (name.contains("_")) {
502+
UnderscoresInNameBehavior behavior = customizationConfig.getUnderscoresInNameBehavior();
502503
List<String> allowedNames = customizationConfig.getAllowedUnderscoreNames();
503504
if (allowedNames != null && allowedNames.contains(name)) {
504505
return;
505506
}
506507

507-
UnderscoresInNameBehavior behavior = customizationConfig.getUnderscoresInNameBehavior();
508-
509508
String supportedBehaviors = Arrays.toString(UnderscoresInNameBehavior.values());
510509
if (behavior == null) {
511510
throw ModelInvalidException.fromEntry(ValidationEntry.create(
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.codegen.validation;
17+
18+
import java.util.Collections;
19+
import java.util.List;
20+
import software.amazon.awssdk.codegen.model.config.customization.CustomizationConfig;
21+
import software.amazon.awssdk.codegen.model.config.customization.UnderscoresInNameBehavior;
22+
23+
public class CustomizationConfigValidator implements ModelValidator{
24+
@Override
25+
public List<ValidationEntry> validateModels(ModelValidationContext context) {
26+
CustomizationConfig config = context.intermediateModel().getCustomizationConfig();
27+
28+
if (config.getUnderscoresInNameBehavior() == UnderscoresInNameBehavior.ALLOW &&
29+
config.getAllowedUnderscoreNames() != null &&
30+
!config.getAllowedUnderscoreNames().isEmpty()) {
31+
32+
return Collections.singletonList(
33+
ValidationEntry.create(
34+
ValidationErrorId.INVALID_CODEGEN_CUSTOMIZATION,
35+
ValidationErrorSeverity.DANGER,
36+
"Cannot set both 'underscoresInNameBehavior=ALLOW' and 'allowedUnderscoreNames'. " +
37+
"Use 'allowedUnderscoreNames' for granular control or 'underscoresInNameBehavior=ALLOW' for all underscores."
38+
)
39+
);
40+
}
41+
42+
return Collections.emptyList();
43+
}
44+
45+
}

codegen/src/test/java/software/amazon/awssdk/codegen/CodeGeneratorTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.junit.jupiter.api.Test;
4242
import software.amazon.awssdk.codegen.internal.Jackson;
4343
import software.amazon.awssdk.codegen.model.config.customization.CustomizationConfig;
44+
import software.amazon.awssdk.codegen.model.config.customization.UnderscoresInNameBehavior;
4445
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
4546
import software.amazon.awssdk.codegen.model.rules.endpoints.EndpointTestSuiteModel;
4647
import software.amazon.awssdk.codegen.model.service.ServiceModel;
@@ -97,6 +98,24 @@ void execute_invokesModelValidators() {
9798
verify(mockValidator).validateModels(any());
9899
}
99100

101+
@Test
102+
void execute_conflictingUnderscoreConfigs_throwsValidationError() {
103+
CustomizationConfig config = CustomizationConfig.create();
104+
config.setAllowedUnderscoreNames(Collections.singletonList("foo_bar"));
105+
config.setUnderscoresInNameBehavior(UnderscoresInNameBehavior.ALLOW);
106+
107+
C2jModels referenceModels = ClientTestModels.awsJsonServiceC2jModels();
108+
109+
C2jModels modelsWithConflict = C2jModels.builder()
110+
.customizationConfig(config)
111+
.serviceModel(referenceModels.serviceModel())
112+
.build();
113+
114+
assertThatThrownBy(() -> generateCodeFromC2jModels(modelsWithConflict, outputDir))
115+
.isInstanceOf(RuntimeException.class)
116+
.hasMessageContaining("Validation failed");
117+
}
118+
100119
@Test
101120
void execute_c2jModelsAndIntermediateModel_generateSameCode() throws IOException {
102121
Path c2jModelsOutputDir = outputDir.resolve("c2jModels");

0 commit comments

Comments
 (0)