Skip to content

Commit

Permalink
Set deprecated for fields in the json schema conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
miguel-vila committed Apr 1, 2024
1 parent 2bdc849 commit 66b3462
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import software.amazon.smithy.model.shapes.TimestampShape;
import software.amazon.smithy.model.shapes.UnionShape;
import software.amazon.smithy.model.traits.DefaultTrait;
import software.amazon.smithy.model.traits.DeprecatedTrait;
import software.amazon.smithy.model.traits.DocumentationTrait;
import software.amazon.smithy.model.traits.EnumTrait;
import software.amazon.smithy.model.traits.LengthTrait;
Expand Down Expand Up @@ -100,13 +101,16 @@ private Schema createRef(MemberShape member) {
if (converter.isInlined(member)) {
return member.accept(this);
} else {
Schema.Builder refBuilder = Schema.builder().ref(converter.toPointer(member.getTarget()));
if (member.hasTrait(DeprecatedTrait.class)) {
refBuilder.deprecated(true);
}
// Wrap the ref and default in an allOf if disableDefaultValues has been not been disabled on config.
if (member.hasTrait(DefaultTrait.class) && !converter.getConfig().getDisableDefaultValues()) {
Schema ref = Schema.builder().ref(converter.toPointer(member.getTarget())).build();
Schema def = Schema.builder().defaultValue(member.expectTrait(DefaultTrait.class).toNode()).build();
return Schema.builder().allOf(ListUtils.of(ref, def)).build();
return Schema.builder().allOf(ListUtils.of(refBuilder.build(), def)).build();
}
return Schema.builder().ref(converter.toPointer(member.getTarget())).build();
return refBuilder.build();
}
}

Expand Down Expand Up @@ -329,6 +333,8 @@ private Schema.Builder updateBuilder(Shape shape, Schema.Builder builder) {
builder.defaultValue(shape.expectTrait(DefaultTrait.class).toNode());
}

shape.getTrait(DeprecatedTrait.class).ifPresent(t -> builder.deprecated(true));

return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public final class Schema implements ToNode, ToSmithyBuilder<Schema> {
private final boolean writeOnly;
private final String comment;
private final Node examples;
private final boolean deprecated;

private final String contentEncoding;
private final String contentMediaType;
Expand Down Expand Up @@ -153,6 +154,7 @@ private Schema(Builder builder) {
writeOnly = builder.writeOnly;
comment = builder.comment;
examples = builder.examples;
deprecated = builder.deprecated;

contentEncoding = builder.contentEncoding;
contentMediaType = builder.contentMediaType;
Expand Down Expand Up @@ -312,6 +314,10 @@ public Optional<Node> getExamples() {
return Optional.ofNullable(examples);
}

public boolean isDeprecated() {
return deprecated;
}

public Optional<String> getContentEncoding() {
return Optional.ofNullable(contentEncoding);
}
Expand Down Expand Up @@ -364,6 +370,7 @@ public Node toNode() {

.withOptionalMember("comment", getComment().map(Node::from))
.withOptionalMember("examples", getExamples())
.withOptionalMember("deprecated", this.deprecated ? Optional.of(Node.from(true)) : Optional.empty())
.withOptionalMember("title", getTitle().map(Node::from))
.withOptionalMember("description", getDescription().map(Node::from))
.withOptionalMember("format", getFormat().map(Node::from))
Expand Down Expand Up @@ -540,6 +547,7 @@ public Builder toBuilder() {
.writeOnly(writeOnly)
.comment(comment)
.examples(examples)
.deprecated(deprecated)

.contentEncoding(contentEncoding)
.contentMediaType(contentMediaType);
Expand Down Expand Up @@ -611,6 +619,7 @@ public static final class Builder implements SmithyBuilder<Schema> {
private boolean writeOnly;
private String comment;
private Node examples;
private boolean deprecated;

private String contentEncoding;
private String contentMediaType;
Expand Down Expand Up @@ -853,6 +862,11 @@ public Builder examples(Node examples) {
return this;
}

public Builder deprecated(boolean deprecated) {
this.deprecated = deprecated;
return this;
}

public Builder extensions(Map<String, Node> extensions) {
this.extensions.clear();
this.extensions.putAll(extensions);
Expand Down Expand Up @@ -945,6 +959,8 @@ public Builder disableProperty(String propertyName) {
return this.contentMediaType(null);
case "examples":
return this.examples(null);
case "deprecated":
return this.deprecated(false);
default:
LOGGER.warning("Unknown JSON Schema config 'disable' property: " + propertyName);
return this;
Expand Down

0 comments on commit 66b3462

Please sign in to comment.