Skip to content

Commit

Permalink
bugfix: data format did not correctly serialize JSON array.
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Aug 19, 2020
1 parent a459832 commit 28c1561
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<artifactId>reedelk-openapi</artifactId>
<groupId>com.reedelk</groupId>
<version>1.0.2-SNAPSHOT</version><!-- Follows the version of the runtime -->
<version>1.0.2</version>

<properties>
<json.version>20190722</json.version>
Expand Down
26 changes: 15 additions & 11 deletions src/main/java/com/reedelk/openapi/commons/DataFormat.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.reedelk.openapi.commons;

import org.json.JSONArray;
import org.json.JSONObject;
import org.yaml.snakeyaml.Yaml;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public enum DataFormat {
Expand Down Expand Up @@ -34,20 +34,24 @@ public boolean is(String dataAsString) {
try {
new JSONObject(dataAsString);
return true;
} catch (Exception exception) {
return false;
} catch (Exception ignore) {
// not a JSON object
}
try {
new JSONArray(dataAsString);
return true;
} catch (Exception ignore) {
// not a JSON array
}
return false;
}

@SuppressWarnings("unchecked")
@Override
public String dump(Object object) {
// If the input object is a linked has map we must create a new HashMap,
// otherwise the JSON object does not serialize correctly.
if (object instanceof LinkedHashMap) {
Map<Object, Object> map = new HashMap<>();
((Map<Object, Object>) object).forEach(map::put);
return new JSONObject(map).toString(2);
if (object instanceof List) {
return new JSONArray((List<?>) object).toString(2);
} else if (object instanceof Map) {
return new JSONObject((Map<?,?>) object).toString(2);
} else {
return new JSONObject(object).toString(2);
}
Expand Down
78 changes: 78 additions & 0 deletions src/test/java/com/reedelk/openapi/commons/DataFormatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.reedelk.openapi.commons;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
import static org.skyscreamer.jsonassert.JSONCompareMode.STRICT;

class DataFormatTest {

@Test
void shouldCorrectlyDumpLinkedHashMapObject() {
// Given
Map<String, Object> shipment = new LinkedHashMap<>();
shipment.put("id", "aabbcc");
shipment.put("details", "FedEx");

// When
String jsonData = DataFormat.JSON.dump(shipment);

// Then
assertEquals("{\n" +
" \"details\": \"FedEx\",\n" +
" \"id\": \"aabbcc\"\n" +
"}", jsonData, STRICT);
}

@Test
void shouldCorrectlyDumpLinkedHashMapObjectWithNestedMap() {
// Given
Map<String, Object> shipmentDetails = new LinkedHashMap<>();
shipmentDetails.put("carrier", "FedEx");
shipmentDetails.put("time", "Morning");

Map<String, Object> shipment = new LinkedHashMap<>();
shipment.put("id", "aabbcc");
shipment.put("details", shipmentDetails);

// When
String jsonData = DataFormat.JSON.dump(shipment);

// Then
assertEquals("{\n" +
" \"details\": {\n" +
" \"carrier\": \"FedEx\",\n" +
" \"time\": \"Morning\"\n" +
" },\n" +
" \"id\": \"aabbcc\"\n" +
"}", jsonData, STRICT);
}

@Test
void shouldCorrectlyDumpArrayAsJson() {
// Given
Map<String, Object> shipment1Details = new LinkedHashMap<>();
shipment1Details.put("carrier", "FedEx");

Map<String, Object> shipment1 = new LinkedHashMap<>();
shipment1.put("id", "aabbcc");
shipment1.put("details", shipment1Details);

List<Map<String, Object>> shipments = new ArrayList<>();
shipments.add(shipment1);

// When
String jsonData = DataFormat.JSON.dump(shipments);

// Then
assertEquals("[{\n" +
" \"details\": {\"carrier\": \"FedEx\"},\n" +
" \"id\": \"aabbcc\"\n" +
"}]", jsonData, STRICT);
}
}

0 comments on commit 28c1561

Please sign in to comment.