Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow serializing prelude in the node serializer #955

Merged
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 @@ -54,7 +54,11 @@ public final class ModelSerializer {

private ModelSerializer(Builder builder) {
metadataFilter = builder.metadataFilter;
shapeFilter = builder.shapeFilter.and(FunctionalUtils.not(Prelude::isPreludeShape));
if (!builder.includePrelude) {
shapeFilter = builder.shapeFilter.and(FunctionalUtils.not(Prelude::isPreludeShape));
} else {
shapeFilter = builder.shapeFilter;
}
traitFilter = builder.traitFilter;
}

Expand Down Expand Up @@ -97,6 +101,7 @@ public static Builder builder() {
public static final class Builder implements SmithyBuilder<ModelSerializer> {
private Predicate<String> metadataFilter = FunctionalUtils.alwaysTrue();
private Predicate<Shape> shapeFilter = FunctionalUtils.alwaysTrue();
private boolean includePrelude = false;
private Predicate<Trait> traitFilter = FunctionalUtils.alwaysTrue();

private Builder() {}
Expand All @@ -121,6 +126,25 @@ public Builder shapeFilter(Predicate<Shape> shapeFilter) {
return this;
}

/**
* Enables or disables including the prelude in the serialized model.
*
* <p>By default, the prelude is not included.
*
* <p>This should nearly always be left at default, as per the spec the prelude is
* inherently part of every model, and so any Smithy implementation must build in
* an understanding of the prelude. Disabling this filter can be useful for those
* implementations to allow them to build their understanding of it from a JSON
* version of the prelude.
*
* @param includePrelude boolean indicating whether the prelude should be included or not.
* @return Returns the builder.
*/
public Builder includePrelude(boolean includePrelude) {
this.includePrelude = includePrelude;
return this;
}

/**
* Sets a predicate that can be used to filter trait values from
* appearing in the serialized model.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Optional;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -141,7 +142,23 @@ public void doesNotSerializePreludeTraitsOrShapes() {
ModelSerializer serializer = ModelSerializer.builder().build();
ObjectNode serialized = serializer.serialize(model);

assertFalse(serialized.getMember("smithy.api").isPresent());
ObjectNode shapes = serialized.expectObjectMember("shapes");
shapes.getMembers().forEach((key, value) -> {
assertThat(key.getValue(), not(startsWith("smithy.api#")));
});
}

@Test
public void allowsDisablingPreludeFilter() {
Model model = Model.assembler().assemble().unwrap();
ModelSerializer serializer = ModelSerializer.builder().includePrelude(true).build();
ObjectNode serialized = serializer.serialize(model);

ObjectNode shapes = serialized.expectObjectMember("shapes");
assertTrue(shapes.getMembers().size() > 1);
shapes.getMembers().forEach((key, value) -> {
assertThat(key.getValue(), startsWith("smithy.api#"));
});
}

@Test
Expand Down