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 @@ -11,6 +11,7 @@ alloy.NullableTrait$Provider
alloy.OffsetDateTimeFormatTrait$Provider
alloy.OffsetTimeFormatTrait$Provider
alloy.OpenEnumTrait$Provider
alloy.PreserveKeyOrderTrait$Provider
alloy.SimpleRestJsonTrait$Provider
alloy.StructurePatternTrait$Provider
alloy.UncheckedExamplesTrait$Provider
Expand Down
1 change: 1 addition & 0 deletions modules/core/resources/META-INF/smithy/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ unions.smithy
urlform.smithy
uuid.smithy
metadata.smithy
map.smithy
15 changes: 15 additions & 0 deletions modules/core/resources/META-INF/smithy/map.smithy
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
$version: "2"

namespace alloy

/// This trait denotes that the order of keys in a map should be preserved
/// when being serialized and deserialized
@trait(
selector: ":test(
map,
member > map,
document,
member > document
)"
)
structure preserveKeyOrder {}
1 change: 1 addition & 0 deletions modules/core/resources/META-INF/smithy/restjson.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace alloy
alloy#dateFormat
alloy#localTimeFormat
alloy#offsetDateTimeFormat
alloy#preserveKeyOrder
]
)
@trait(selector: "service")
Expand Down
46 changes: 46 additions & 0 deletions modules/core/src/alloy/PreserveKeyOrderTrait.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* Copyright 2022 Disney Streaming
*
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://disneystreaming.github.io/TOST-1.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 alloy;

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;
import software.amazon.smithy.model.traits.AbstractTrait;

public class PreserveKeyOrderTrait extends AnnotationTrait {

public static ShapeId ID = ShapeId.from("alloy#preserveKeyOrder");

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

public PreserveKeyOrderTrait() {
super(ID, Node.objectNode());
}

public static final class Provider extends AbstractTrait.Provider {
public Provider() {
super(ID);
}

@Override
public PreserveKeyOrderTrait createTrait(ShapeId target, Node node) {
return new PreserveKeyOrderTrait(node.expectObjectNode());
}
}
}
2 changes: 1 addition & 1 deletion modules/core/test/resources/META-INF/smithy/manifest
Original file line number Diff line number Diff line change
@@ -1 +1 @@
traits.smithy
traits.smithy
17 changes: 17 additions & 0 deletions modules/core/test/resources/META-INF/smithy/traits.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ use alloy#LocalDate
use alloy#YearMonth
use alloy#MonthDay
use alloy#OffsetDateTime
use alloy#preserveKeyOrder

@dateFormat
string MyDate
Expand Down Expand Up @@ -258,6 +259,22 @@ map UnknownProps {
value: Document
}

@preserveKeyOrder
document MyDocument

@preserveKeyOrder
map TestOrderedMap {
key: String
value: String
}

structure StructureWithOrderedMapMember {
@preserveKeyOrder
foo: UnknownProps
@preserveKeyOrder
bar: Document
}

@localTimeFormat
string MyLocalTime

Expand Down
21 changes: 20 additions & 1 deletion modules/protocol-tests/resources/META-INF/smithy/Pizza.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ namespace alloy.test
use alloy#simpleRestJson
use alloy#jsonUnknown
use alloy#discriminated
use alloy#preserveKeyOrder

@simpleRestJson
service PizzaAdminService {
version: "1.0.0",
errors: [GenericServerError, GenericClientError],
operations: [AddMenuItem, GetMenu, Version, Health, HeaderEndpoint, RoundTrip, GetEnum, GetIntEnum, CustomCode, HttpPayloadWithDefault, HttpPayloadRequiredWithDefault, OpenUnions, Primitives]
operations: [AddMenuItem, GetMenu, Version, Health, HeaderEndpoint, RoundTrip, GetEnum, GetIntEnum, CustomCode, HttpPayloadWithDefault, HttpPayloadRequiredWithDefault, OpenUnions, Primitives, PreserveOrder]
}

@http(method: "POST", uri: "/restaurant/{restaurant}/menu/item", code: 201)
Expand Down Expand Up @@ -381,3 +382,21 @@ structure PrimitiveEncodings {
@required
offsetDateTime: alloy#OffsetDateTime
}

@preserveKeyOrder
map MyMap {
key: String,
value: Integer
}

@http(uri: "/preserveKeyOrder", method: "POST", code: 200)
operation PreserveOrder {
input: PreserveOrderStruct
output: PreserveOrderStruct
}

structure PreserveOrderStruct {
map: MyMap
@preserveKeyOrder
document: Document
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
$version: "2"

namespace alloy.test

use alloy#simpleRestJson
use alloy.test#PreserveOrder
use smithy.test#httpRequestTests
use smithy.test#httpResponseTests

apply PreserveOrder @httpRequestTests([
{
id:"PreserveKeyOrderRequest"
protocol: simpleRestJson
uri: "/preserveKeyOrder"
method: "POST"
body : """
{"map":{"a":1,"d":2,"e":3,"b":4},"document":{"foo":1,"a":"b","c":[],"bar":null}}"""
params: {
map: {
"a": 1,
"d": 2,
"e": 3,
"b": 4
},
document: {
"foo": 1,
"a": "b",
"c": [],
"bar": null
}
}
}
])

apply PreserveOrder @httpResponseTests([
{
id: "PreserveKeyOrderResponse"
protocol: simpleRestJson
uri: "/preserveKeyOrder"
method: "POST"
code: 200
body : """
{"map":{"a":1,"d":2,"e":3,"b":4},"document":{"foo":1,"a":"b","c":[],"bar":null}}"""
params: {
map: {
"a": 1,
"d": 2,
"e": 3,
"b": 4
},
document: {
"foo": 1,
"a": "b",
"c": [],
"bar": null
}
}
}
])
1 change: 1 addition & 0 deletions modules/protocol-tests/resources/META-INF/smithy/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ Version.smithy
HttpPayloadWithDefault.smithy
test-config.json
OpenUnions.smithy
PreserveKeyOrder.smithy
routing/Routing.smithy
routing/RoutingSpec.smithy