Update ValidationError#toString to print the extentions field #3024
Conversation
- Updated toString method in ValidationError class to print the extensions field. If the field is null then an empty array will be printed `...extensions=[]` - Added a couple of UTs to test the ValidationError#toString method
…eryPath and extensions fields
….com:federicorispo/graphql-java into chore/fix-tostring-method-in-validationerror
| } | ||
|
|
||
| if (builder.extensions != null) { | ||
| this.extensions.putAll(builder.extensions); |
There was a problem hiding this comment.
This is the only point I don't like because the putAll method does not maintain the order
| private final List<String> queryPath; | ||
| private final Map<String, Object> extensions; | ||
| private final List<String> queryPath = new ArrayList<>(); | ||
| private final Map<String, Object> extensions = new HashMap<>(); |
There was a problem hiding this comment.
One way to make this class consistent (in the field initializations part) is to initialize also the queryPath and the extensions (like I have done): this lead to the putAll problem that does not maintain the order of the keys. The other way is to remove this initialization. Let me know what do you think about it
There was a problem hiding this comment.
Make it a ImmutableMap.of() under the covers
private final ImmutableMap<String, Object> extensions;
and then do this.extensions = ImmutableMap.of(builder.extensions)
If it was already a immutable map then its a smart coy - if not it keeps order - ImmutableMaps are like LinkedHashMap in that sense
There was a problem hiding this comment.
This method ImmutableMap.of(Map<> map) does not exist so I used the ternary operator to initialize the extensions field with .copyOf() when the builder.extension is not null, otherwise the extensions is initialized with the .of() method
| this.queryPath.addAll(builder.queryPath); | ||
| } | ||
|
|
||
| this.extensions = (builder.extensions != null) ? ImmutableMap.copyOf(builder.extensions) : ImmutableMap.of(); |
toStringmethod inValidationErrorclass to print theextensionsfield. If the field is null then an empty array will be printed...extensions=[]messagefield because it was always initialized with the description but the getMessage() method is still there and returns the content of the descriptionextensionsandqueryPathfields during their declaration to align with thesourceLocationsfieldIt resolves #3005