Skip to content

Commit

Permalink
#587 fix hashCode NPE for null scalars
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Dec 20, 2023
1 parent b5450a7 commit f44267a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
13 changes: 10 additions & 3 deletions src/main/java/com/amihaiemil/eoyaml/BaseScalar.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ public boolean equals(final Object other) {
*/
@Override
public int hashCode() {
return this.value().hashCode();
final int hashCode;
final String value = this.value();
if(value == null) {
hashCode = 0;
} else {
hashCode = value.hashCode();
}
return hashCode;
}

/**
Expand Down Expand Up @@ -98,10 +105,10 @@ public int compareTo(final YamlNode other) {
result = 0;
} else if(value != null && otherVal == null) {
result = 1;
} else if (value == null && otherVal != null) {
} else if (value == null) {
result = -1;
} else {
result = this.value().compareTo(otherVal);
result = value.compareTo(otherVal);
}
}
return result;
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/amihaiemil/eoyaml/JsonYamlMapping.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.amihaiemil.eoyaml;

import javax.json.JsonObject;
import javax.json.JsonValue;
import java.util.LinkedHashSet;
import java.util.Set;

Expand Down Expand Up @@ -36,8 +35,14 @@ public Set<YamlNode> keys() {

@Override
public YamlNode value(final YamlNode key) {
final JsonValue jsonValue = this.object.get(key.asScalar().value());
return new JsonYamlDump(jsonValue).dump();
final String scalar = key.asScalar().value();
final YamlNode value;
if(scalar == null) {
value = null;
} else {
value = new JsonYamlDump(this.object.get(scalar)).dump();
}
return value;
}

@Override
Expand Down
33 changes: 21 additions & 12 deletions src/main/java/com/amihaiemil/eoyaml/RtYamlPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,12 @@ private String indent(final String value, final int indentation) {
alignment.append(" ");
spaces--;
}
String[] lines = value.split(System.lineSeparator());
String[] lines;
if(value == null) {
lines = new String[]{"null"};
} else {
lines = value.split(System.lineSeparator());
}
StringBuilder printed = new StringBuilder();
for(int idx = 0; idx < lines.length; idx++) {
printed.append(alignment);
Expand All @@ -367,6 +372,7 @@ private String indent(final String value, final int indentation) {
* @author Mihai Andronache ([email protected])
* @version $Id$
* @since 4.3.1
* @checkstyle LineLength (100 lines)
*/
static class Escaped extends BaseScalar {

Expand All @@ -384,22 +390,25 @@ static class Escaped extends BaseScalar {
}

@Override


public String value() {
final String value = this.original.value();
String escaped = value;
boolean quoted = (value.startsWith("'") && value.endsWith("'"))
|| (value.startsWith("\"") && value.endsWith("\""));
final String regex = ".*[?\\-#:>|$%&{}\\[\\]@`!*,'\"]+.*|[ ]+";
if (!quoted && value.matches(regex)) {
if(value.contains("\"")) {
escaped = "'" + value + "'";
String toEscape;
if(value == null) {
toEscape = "null";
} else {
toEscape = value;
}
boolean alreadyEscaped = (toEscape.startsWith("'") && toEscape.endsWith("'"))
|| (toEscape.startsWith("\"") && toEscape.endsWith("\""));
final String needsEscaping = ".*[?\\-#:>|$%&{}\\[\\]@`!*,'\"]+.*|[ ]+|null";
if (!alreadyEscaped && toEscape.matches(needsEscaping)) {
if(toEscape.contains("\"")) {
toEscape = "'" + toEscape + "'";
} else {
escaped = "\"" + value + "\"";
toEscape = "\"" + toEscape + "\"";
}
}
return escaped;
return toEscape;
}

@Override
Expand Down

0 comments on commit f44267a

Please sign in to comment.