Skip to content

Commit

Permalink
Fix NPE on trait with implicit null (smithy-lang#1942)
Browse files Browse the repository at this point in the history
  • Loading branch information
srchase authored and alextwoods committed Sep 15, 2023
1 parent cfbc4be commit ec31537
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -328,24 +328,29 @@ Doc visit(TreeCursor cursor) {
.detectHardLines(cursor)
.write();
} else {
// Check the inner trait node for hard line breaks rather than the wrapper.
TreeCursor traitNode = cursor
.getFirstChild(TreeType.TRAIT_NODE)
.getFirstChild(TreeType.NODE_VALUE)
.getFirstChild(); // The actual node value.
return new BracketFormatter()
.open(Formatter.LPAREN)
.close(Formatter.RPAREN)
.extractChildren(cursor, BracketFormatter.extractor(this::visit, child -> {
if (child.getTree().getType() == TreeType.TRAIT_NODE) {
// Split WS and NODE_VALUE so that they appear on different lines.
return child.getChildrenByType(TreeType.NODE_VALUE, TreeType.WS).stream();
} else {
return Stream.empty();
}
}))
.detectHardLines(traitNode)
.write();
if (cursor.getFirstChild(TreeType.TRAIT_NODE) != null) {
// Check the inner trait node for hard line breaks rather than the wrapper.
TreeCursor traitNode = cursor
.getFirstChild(TreeType.TRAIT_NODE)
.getFirstChild(TreeType.NODE_VALUE)
.getFirstChild(); // The actual node value.
return new BracketFormatter()
.open(Formatter.LPAREN)
.close(Formatter.RPAREN)
.extractChildren(cursor, BracketFormatter.extractor(this::visit, child -> {
if (child.getTree().getType() == TreeType.TRAIT_NODE) {
// Split WS and NODE_VALUE so that they appear on different lines.
return child.getChildrenByType(TreeType.NODE_VALUE, TreeType.WS).stream();
} else {
return Stream.empty();
}
}))
.detectHardLines(traitNode)
.write();
} else {
// If the trait node is empty, remove the empty parentheses.
return Doc.text("");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
$version: "2.0"

namespace smithy.example

structure Struct {
@default
a: String

@default
b: String

@default
c: String

@default(null)
d: String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
$version: "2.0"

namespace smithy.example

structure Struct {
@default
a: String

@default()
b: String

@default( )
c: String

@default(null)
d: String
}

0 comments on commit ec31537

Please sign in to comment.