Skip to content

Add disableDefaultDeprecatedMessage configuration option for json schema #2694

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -136,6 +136,7 @@ public String toString() {
private boolean disableIntEnums = false;
private boolean addReferenceDescriptions = false;
private boolean useInlineMaps = false;
private boolean disableDefaultDeprecatedMessage = false;

public JsonSchemaConfig() {
nodeMapper.setWhenMissingSetter(NodeMapper.WhenMissing.IGNORE);
Expand Down Expand Up @@ -467,6 +468,24 @@ public void setDisableIntEnums(boolean disableIntEnums) {
this.disableIntEnums = disableIntEnums;
}

public boolean getDisableDefaultDeprecatedMessage() {
return disableDefaultDeprecatedMessage;
}

/**
* Set to true to disable adding default deprecated messages to schema descriptions
* when the @deprecated trait is present but has no message or since.
*
* <p>By default, when a shape has the @deprecated trait, a default message like
* "This operation is deprecated." is added to the schema description. Setting this
* to true will prevent the default description from being added.
*
* @param disableDefaultDeprecatedMessage True to disable default description for @deprecated trait.
*/
public void setDisableDefaultDeprecatedMessage(boolean disableDefaultDeprecatedMessage) {
this.disableDefaultDeprecatedMessage = disableDefaultDeprecatedMessage;
}

/**
* JSON schema version to use when converting Smithy shapes into Json Schema.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,21 @@ private Optional<String> descriptionMessage(Shape shape) {
shape
.getTrait(DocumentationTrait.class)
.ifPresent(trait -> builder.append(trait.getValue()));

JsonSchemaConfig config = converter.getConfig();
boolean disableDefaultDeprecatedMessage = config.getDisableDefaultDeprecatedMessage();

shape
.getTrait(DeprecatedTrait.class)
.ifPresent(trait -> builder
.append("\n")
.append(trait.getDeprecatedDescription(shape.getType())));
.ifPresent(trait -> {
if (!disableDefaultDeprecatedMessage || trait.getMessage().isPresent()
|| trait.getSince().isPresent()) {
builder
.append("\n")
.append(trait.getDeprecatedDescription(shape.getType()));
}
});

String description = builder.toString().trim();
return description.isEmpty() ? Optional.empty() : Optional.of(description);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,95 @@ public void appendsDeprecatedInfoInDescription() {
assertThat(document.getRootSchema().getDescription().get(), equalTo(expected));
}

@Test
public void disableDefaultDeprecatedMessage() {
IntegerShape shape = IntegerShape.builder()
.id("a.b#C")
.addTrait(DeprecatedTrait.builder().build())
.addTrait(new DocumentationTrait("This is an integer."))
.build();
Model model = Model.builder().addShape(shape).build();
JsonSchemaConfig config = new JsonSchemaConfig();
config.setDisableDefaultDeprecatedMessage(true);
SchemaDocument document = JsonSchemaConverter.builder()
.addMapper(new OpenApiJsonSchemaMapper())
.model(model)
.config(config)
.build()
.convertShape(shape);

String expected = "This is an integer.";
assertThat(document.getRootSchema().getDescription().get(), equalTo(expected));

// Asserts that deprecated node is still there
assertThat(document.getRootSchema().getExtension("deprecated").get(), equalTo(Node.from(true)));
}

@Test
public void disableDefaultDeprecatedMessageStillAllowsCustomMessage() {
String message = "Use a.b#D instead.";
IntegerShape shape = IntegerShape.builder()
.id("a.b#C")
.addTrait(DeprecatedTrait.builder().message(message).build())
.addTrait(new DocumentationTrait("This is an integer."))
.build();
Model model = Model.builder().addShape(shape).build();
JsonSchemaConfig config = new JsonSchemaConfig();
config.setDisableDefaultDeprecatedMessage(true);
SchemaDocument document = JsonSchemaConverter.builder()
.addMapper(new OpenApiJsonSchemaMapper())
.model(model)
.config(config)
.build()
.convertShape(shape);

String expected = "This is an integer.\nThis shape is deprecated: Use a.b#D instead.";
assertThat(document.getRootSchema().getDescription().get(), equalTo(expected));
}

@Test
public void disableDefaultDeprecatedMessageStillAllowsSince() {
String since = "2020-01-01";
IntegerShape shape = IntegerShape.builder()
.id("a.b#C")
.addTrait(DeprecatedTrait.builder().since(since).build())
.addTrait(new DocumentationTrait("This is an integer."))
.build();
Model model = Model.builder().addShape(shape).build();
JsonSchemaConfig config = new JsonSchemaConfig();
config.setDisableDefaultDeprecatedMessage(true);
SchemaDocument document = JsonSchemaConverter.builder()
.addMapper(new OpenApiJsonSchemaMapper())
.model(model)
.config(config)
.build()
.convertShape(shape);

String expected = "This is an integer.\nThis shape is deprecated since 2020-01-01.";
assertThat(document.getRootSchema().getDescription().get(), equalTo(expected));
}

@Test
public void disableDefaultDeprecatedMessageOnlyAffectsIfTrue() {
IntegerShape shape = IntegerShape.builder()
.id("a.b#C")
.addTrait(DeprecatedTrait.builder().build())
.addTrait(new DocumentationTrait("This is an integer."))
.build();
Model model = Model.builder().addShape(shape).build();
JsonSchemaConfig config = new JsonSchemaConfig();
config.setDisableDefaultDeprecatedMessage(false);
SchemaDocument document = JsonSchemaConverter.builder()
.addMapper(new OpenApiJsonSchemaMapper())
.model(model)
.config(config)
.build()
.convertShape(shape);

String expected = "This is an integer.\nThis shape is deprecated.";
assertThat(document.getRootSchema().getDescription().get(), equalTo(expected));
}

@Test
public void supportsInt32() {
IntegerShape shape = IntegerShape.builder().id("a.b#C").build();
Expand Down