Skip to content

Commit 9ed70c1

Browse files
authored
Fix codegen error message when shapes have lowercased names (#5914)
* Fix codegen error message when shapes have lowercased names * Added change log
1 parent 23c3b66 commit 9ed70c1

File tree

16 files changed

+172
-40
lines changed

16 files changed

+172
-40
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "AWS SDK for Java v2",
4+
"contributor": "",
5+
"description": "Improving codegen error message when shapes have lowercased names."
6+
}

codegen/src/main/java/software/amazon/awssdk/codegen/customization/processors/DefaultCustomizationProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public static CodegenCustomizationProcessor getProcessorFor(
4040
new NewAndLegacyEventStreamProcessor(),
4141
new S3RemoveBucketFromUriProcessor(),
4242
new S3ControlRemoveAccountIdHostPrefixProcessor(),
43-
new ExplicitStringPayloadQueryProtocolProcessor()
43+
new ExplicitStringPayloadQueryProtocolProcessor(),
44+
new LowercaseShapeValidatorProcessor()
4445
);
4546
}
4647
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.customization.processors;
17+
18+
import software.amazon.awssdk.codegen.customization.CodegenCustomizationProcessor;
19+
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
20+
import software.amazon.awssdk.codegen.model.service.ServiceModel;
21+
22+
/**
23+
* A processor that validates shape names in service models to ensure they start with uppercase letters.
24+
* This validation is necessary because shapes of type "structure" are converted to Java classes,
25+
* which must start with uppercase letters according to Java naming conventions.
26+
*/
27+
public class LowercaseShapeValidatorProcessor implements CodegenCustomizationProcessor {
28+
29+
@Override
30+
public void preprocess(ServiceModel serviceModel) {
31+
32+
serviceModel.getShapes().forEach((shapeName, shape) -> {
33+
if ("structure".equals(shape.getType()) && Character.isLowerCase(shapeName.charAt(0))) {
34+
throw new IllegalStateException(
35+
String.format("Shape name '%s' starts with a lowercase character." +
36+
"Shape names must start with an uppercase character." +
37+
"Please update the shape name in your service model",
38+
shapeName));
39+
}
40+
});
41+
}
42+
43+
@Override
44+
public void postprocess(IntermediateModel intermediateModel) {
45+
// no-op
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.customization.processors;
17+
18+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
19+
20+
import java.io.File;
21+
import java.io.IOException;
22+
import org.junit.jupiter.api.BeforeAll;
23+
import org.junit.jupiter.api.Test;
24+
import software.amazon.awssdk.codegen.model.service.ServiceModel;
25+
import software.amazon.awssdk.codegen.utils.ModelLoaderUtils;
26+
27+
public class LowercaseShapeValidatorProcessorTest {
28+
29+
private static ServiceModel serviceModel;
30+
private final LowercaseShapeValidatorProcessor processor = new LowercaseShapeValidatorProcessor();
31+
32+
@BeforeAll
33+
public static void setUp() throws IOException {
34+
File serviceModelFile = new File(LowercaseShapeValidatorProcessorTest.class
35+
.getResource("/software/amazon/awssdk/codegen/poet/client/c2j/lower-case-shape"
36+
+ "-validator/service-2.json").getFile());
37+
serviceModel = ModelLoaderUtils.loadModel(ServiceModel.class, serviceModelFile);
38+
}
39+
40+
@Test
41+
public void preprocess_serviceWithLowercaseShape_throwsException() {
42+
assertThatThrownBy(() -> processor.preprocess(serviceModel))
43+
.isInstanceOf(IllegalStateException.class)
44+
.hasMessageContaining("Shape name 'lowercaseshape' starts with a lowercase character");
45+
}
46+
}

codegen/src/test/resources/software/amazon/awssdk/codegen/emitters/customizations/processors/uselegacyeventgenerationscheme/service-2.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@
178178
],
179179
"members": {
180180
"SomeNestedMember": {
181-
"shape": "nestedMember",
181+
"shape": "NestedMember",
182182
"documentation": "<p>a member that has nested members</p>"
183183
},
184184
"OptionalMember": {
@@ -196,7 +196,7 @@
196196
"type": "structure",
197197
"members": {
198198
"SomeNestedMember": {
199-
"shape": "nestedMember",
199+
"shape": "NestedMember",
200200
"documentation": "<p>a member that has nested members</p>"
201201
},
202202
"OptionalMember": {
@@ -212,7 +212,7 @@
212212
],
213213
"members": {
214214
"SomeNestedMember": {
215-
"shape": "nestedMember",
215+
"shape": "NestedMember",
216216
"documentation": "<p>a member that has nested members</p>"
217217
},
218218
"OptionalMember": {
@@ -228,7 +228,7 @@
228228
],
229229
"members": {
230230
"NestedMember": {
231-
"shape": "nestedMember",
231+
"shape": "NestedMember",
232232
"documentation": "<p>A structure containing nested members</p>"
233233
}
234234
},
@@ -249,7 +249,7 @@
249249
},
250250
"exception": true
251251
},
252-
"nestedMember": {
252+
"NestedMember": {
253253
"type": "structure",
254254
"required": [
255255
"SubMember",

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/c2j/json-bearer-auth/service-2.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
],
4444
"members": {
4545
"SomeNestedMember": {
46-
"shape": "nestedMember",
46+
"shape": "NestedMember",
4747
"documentation": "<p>a member that has nested members</p>"
4848
},
4949
"OptionalMember": {
@@ -72,7 +72,7 @@
7272
},
7373
"exception": true
7474
},
75-
"nestedMember": {
75+
"NestedMember": {
7676
"type": "structure",
7777
"required": [
7878
"SubMember",

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/c2j/json/service-2.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@
208208
],
209209
"members": {
210210
"SomeNestedMember": {
211-
"shape": "nestedMember",
211+
"shape": "NestedMember",
212212
"documentation": "<p>a member that has nested members</p>"
213213
},
214214
"OptionalMember": {
@@ -226,7 +226,7 @@
226226
"type": "structure",
227227
"members": {
228228
"SomeNestedMember": {
229-
"shape": "nestedMember",
229+
"shape": "NestedMember",
230230
"documentation": "<p>a member that has nested members</p>"
231231
},
232232
"OptionalMember": {
@@ -242,7 +242,7 @@
242242
],
243243
"members": {
244244
"SomeNestedMember": {
245-
"shape": "nestedMember",
245+
"shape": "NestedMember",
246246
"documentation": "<p>a member that has nested members</p>"
247247
},
248248
"OptionalMember": {
@@ -258,7 +258,7 @@
258258
],
259259
"members": {
260260
"NestedMember": {
261-
"shape": "nestedMember",
261+
"shape": "NestedMember",
262262
"documentation": "<p>A structure containing nested members</p>"
263263
}
264264
},
@@ -281,7 +281,7 @@
281281
"exception": true,
282282
"fault": true
283283
},
284-
"nestedMember": {
284+
"NestedMember": {
285285
"type": "structure",
286286
"required": [
287287
"SubMember",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"version": "2.0",
3+
"metadata": {
4+
"serviceId": "LowercaseShape",
5+
"serviceFullName": "Lowercase Shape Validation Test Service"
6+
},
7+
"operations": {
8+
"TestOperation": {
9+
"name": "TestOperation",
10+
"http": {
11+
"method": "POST",
12+
"requestUri": "/"
13+
},
14+
"input": {"shape": "lowercaseshape"}
15+
}
16+
},
17+
"shapes": {
18+
"lowercaseshape": {
19+
"type": "structure",
20+
"members": {
21+
"StringMember": {
22+
"shape": "String"
23+
}
24+
}
25+
},
26+
"String": {
27+
"type": "string"
28+
}
29+
}
30+
}

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/c2j/query-to-json-errorcode/service-2.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
],
4545
"members": {
4646
"SomeNestedMember": {
47-
"shape": "nestedMember",
47+
"shape": "NestedMember",
4848
"documentation": "<p>a member that has nested members</p>"
4949
},
5050
"OptionalMember": {
@@ -73,7 +73,7 @@
7373
},
7474
"exception": true
7575
},
76-
"nestedMember": {
76+
"NestedMember": {
7777
"type": "structure",
7878
"required": [
7979
"SubMember",

0 commit comments

Comments
 (0)