Skip to content

Commit

Permalink
Fix an issue loading jsonAdd map from config
Browse files Browse the repository at this point in the history
  • Loading branch information
kstich committed Dec 17, 2020
1 parent f8a53c0 commit 0e6b8db
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
12 changes: 7 additions & 5 deletions docs/source/1.0/guides/generating-cloudformation-resources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,13 @@ jsonAdd (``Map<String, Map<String, Node>>``)
"service": "smithy.example#Queues",
"organizationName": "Smithy",
"jsonAdd": {
"/info/title": "Replaced title value",
"/info/nested/foo": {
"hi": "Adding this object created intermediate objects too!"
},
"/info/nested/foo/baz": true
"smithy.example#Queue": {
"/info/title": "Replaced title value",
"/info/nested/foo": {
"hi": "Adding this object created intermediate objects too!"
},
"/info/nested/foo/baz": true
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package software.amazon.smithy.aws.cloudformation.schema;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -24,6 +25,7 @@
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.NodeMapper;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.node.StringNode;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.utils.ListUtils;

Expand Down Expand Up @@ -304,6 +306,18 @@ public static CfnConfig fromNode(Node settings) {
CfnConfig config = new CfnConfig();
mapper.deserializeInto(node, config);

// Load a ShapeId map for the jsonAdd setting.
node.getObjectMember("jsonAdd").ifPresent(jsonAddNode -> {
Map<ShapeId, Map<String, Node>> jsonAddMap = new HashMap<>();

for (Map.Entry<StringNode, Node> jsonAddMember : jsonAddNode.getMembers().entrySet()) {
jsonAddMap.put(ShapeId.from(jsonAddMember.getKey().getValue()),
jsonAddMember.getValue().expectObjectNode().getStringMap());
}

config.setJsonAdd(jsonAddMap);
});

// Add all properties to "extensions" to make them accessible
// in plugins.
for (Map.Entry<String, Node> entry : node.getStringMap().entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.aws.cloudformation.schema.CfnConfig;
import software.amazon.smithy.aws.cloudformation.schema.CfnException;
import software.amazon.smithy.jsonschema.JsonSchemaConfig;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.NodePointer;
import software.amazon.smithy.model.node.ObjectNode;

public class CfnConfigTest {
@Test
Expand Down Expand Up @@ -57,4 +62,36 @@ public void throwsOnDifferentUnionStrategy() {
assertThrows(CfnException.class, () -> config.setUnionStrategy(JsonSchemaConfig.UnionStrategy.OBJECT));
assertThrows(CfnException.class, () -> config.setUnionStrategy(JsonSchemaConfig.UnionStrategy.STRUCTURE));
}

@Test
public void handlesFromNode() {
Model model = Model.assembler()
.addImport(CfnConfigTest.class.getResource("mappers/simple.smithy"))
.discoverModels()
.assemble()
.unwrap();

ObjectNode addNode = Node.objectNodeBuilder()
.withMember("/arbitrary/foo", "whoa")
.build();

ObjectNode configNode = Node.objectNodeBuilder()
.withMember("organizationName", "Smithy")
.withMember("service", "smithy.example#TestService")
.withMember("jsonAdd", Node.objectNodeBuilder()
.withMember("smithy.example#FooResource", addNode)
.build())
.build();

CfnConfig config = CfnConfig.fromNode(configNode);

ObjectNode resourceNode = CfnConverter.create()
.config(config)
.convertToNodes(model)
.get("Smithy::TestService::FooResource");

String arbitraryFoo = NodePointer.parse("/arbitrary/foo").getValue(resourceNode).expectStringNode().getValue();

Assertions.assertEquals("whoa", arbitraryFoo);
}
}

0 comments on commit 0e6b8db

Please sign in to comment.