|
32 | 32 | import software.amazon.smithy.model.shapes.Shape;
|
33 | 33 | import software.amazon.smithy.model.shapes.ShapeId;
|
34 | 34 | import software.amazon.smithy.model.shapes.ShapeType;
|
| 35 | +import software.amazon.smithy.model.traits.BoxTrait; |
35 | 36 | import software.amazon.smithy.model.traits.EnumTrait;
|
36 | 37 | import software.amazon.smithy.model.traits.StreamingTrait;
|
37 | 38 |
|
@@ -75,13 +76,13 @@ public class Native extends NameResolver {
|
75 | 76 | NATIVE_TYPES_BY_SIMPLE_SHAPE_TYPE =
|
76 | 77 | Map.ofEntries(
|
77 | 78 | Map.entry(ShapeType.BLOB, ClassName.get(ByteBuffer.class)),
|
78 |
| - Map.entry(ShapeType.BOOLEAN, TypeName.BOOLEAN.box()), |
79 |
| - Map.entry(ShapeType.BYTE, TypeName.BYTE.box()), |
80 |
| - Map.entry(ShapeType.SHORT, TypeName.SHORT.box()), |
81 |
| - Map.entry(ShapeType.INTEGER, TypeName.INT.box()), |
82 |
| - Map.entry(ShapeType.LONG, TypeName.LONG.box()), |
83 |
| - Map.entry(ShapeType.FLOAT, TypeName.FLOAT.box()), |
84 |
| - Map.entry(ShapeType.DOUBLE, TypeName.DOUBLE.box()), |
| 79 | + Map.entry(ShapeType.BOOLEAN, TypeName.BOOLEAN), |
| 80 | + Map.entry(ShapeType.BYTE, TypeName.BYTE), |
| 81 | + Map.entry(ShapeType.SHORT, TypeName.SHORT), |
| 82 | + Map.entry(ShapeType.INTEGER, TypeName.INT), |
| 83 | + Map.entry(ShapeType.LONG, TypeName.LONG), |
| 84 | + Map.entry(ShapeType.FLOAT, TypeName.FLOAT), |
| 85 | + Map.entry(ShapeType.DOUBLE, TypeName.DOUBLE), |
85 | 86 | // TODO: AWS SDK V2 uses Instant, not Date
|
86 | 87 | Map.entry(ShapeType.TIMESTAMP, ClassName.get(Date.class)),
|
87 | 88 | Map.entry(ShapeType.BIG_DECIMAL, ClassName.get(BigDecimal.class)),
|
@@ -143,14 +144,21 @@ public TypeName typeForShape(final ShapeId shapeId) {
|
143 | 144 | }
|
144 | 145 |
|
145 | 146 | return switch (shape.getType()) {
|
146 |
| - // All primitives are boxed for safety, even if @required |
147 |
| - case BOOLEAN, |
148 |
| - BYTE, |
149 |
| - SHORT, |
150 |
| - INTEGER, |
151 |
| - LONG, |
152 |
| - FLOAT, |
153 |
| - DOUBLE -> NATIVE_TYPES_BY_SIMPLE_SHAPE_TYPE.get(shape.getType()); |
| 147 | + case BOOLEAN, BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE -> { |
| 148 | + /* From the Smithy Docs: |
| 149 | + * "Boolean, byte, short, integer, long, float, and double shapes |
| 150 | + * are only considered boxed if they are marked with the box trait. |
| 151 | + * All other shapes are always considered boxed." |
| 152 | + * https://smithy.io/1.0/spec/core/type-refinement-traits.html#box-trait |
| 153 | + * While Smithy Models SHOULD use Smithy Prelude shapes to avoid this confusion, |
| 154 | + * they do not have to. |
| 155 | + * Hence, the need to check if these shapes have the box trait |
| 156 | + */ |
| 157 | + final TypeName typeName = NATIVE_TYPES_BY_SIMPLE_SHAPE_TYPE.get( |
| 158 | + shape.getType() |
| 159 | + ); |
| 160 | + yield shape.hasTrait(BoxTrait.class) ? typeName.box() : typeName; |
| 161 | + } |
154 | 162 | // For supported simple shapes, just map to native types
|
155 | 163 | case BLOB,
|
156 | 164 | TIMESTAMP,
|
|
0 commit comments