Skip to content

Commit

Permalink
fix duplicate array element on root
Browse files Browse the repository at this point in the history
  • Loading branch information
mfatihercik committed May 5, 2023
1 parent 9d351aa commit 5f8b0c0
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ public void setValue(ParsingContext parsingContext, Node currentNode, ParsingEle
currentNode.getParent().add(currentNode.getData());
list.add(currentNode.toObject(parsingContext.getResultType()));
} else {
currentNode.getParent().add(currentNode.getData());
list.add(currentNode.getData());
currentNode.setClose(true);
// it is already added to the list
if (!currentNode.isClose()) {
currentNode.getParent().add(currentNode.getData());
list.add(currentNode.getData());
currentNode.setClose(true);
}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ protected void registerDefaultConverter() {
typeConverter.put("bigdecimal", new BigDecimalTypeConverter());
typeConverter.put("biginteger", new BigIntegerTypeConverter());
typeConverter.put("default", new DefaultTypeConverter());
typeConverter.put("string", new DefaultTypeConverter());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,82 @@ import java.nio.file.Paths
class JsonParserSpec extends Specification {

private static final String pathPlace = "/configs/parsing"
private static final Path rootPath = Paths.get (TestUtils.getTestResourcePath (), pathPlace)
private static final Path rootPath = Paths.get(TestUtils.getTestResourcePath(), pathPlace)

def "pet Store Json parsing"() throws IOException {

TestMemoryLog memoryLog = new TestMemoryLog ()
memoryLog.logBefore ()
TestMemoryLog memoryLog = new TestMemoryLog()
memoryLog.logBefore()

Path rootPath = Paths.get (TestUtils.getTestResourcePath (), pathPlace)
Path rootPath = Paths.get(TestUtils.getTestResourcePath(), pathPlace)

DSMBuilder builder = new DSMBuilder (rootPath.resolve ("pet-store.yaml").toFile (), rootPath.toString ())
DSM dsm = builder.create ()
DSMBuilder builder = new DSMBuilder(rootPath.resolve("pet-store.yaml").toFile(), rootPath.toString())
DSM dsm = builder.create()


when:
def object = dsm.toObject (rootPath.resolve ("swagger-pet-store.json").toFile ())
def object = dsm.toObject(rootPath.resolve("swagger-pet-store.json").toFile())
//
memoryLog.logAfter ()
memoryLog.print ()
memoryLog.gc ()
memoryLog.logAfter()
memoryLog.print()
memoryLog.gc()

then:
StaxParserSpec.petStoreTest (object)
StaxParserSpec.petStoreTest(object)

}

def "array root should add object once"() throws IOException {

TestMemoryLog memoryLog = new TestMemoryLog()
memoryLog.logBefore()

Path rootPath = Paths.get(TestUtils.getTestResourcePath(), pathPlace)

DSMBuilder builder = new DSMBuilder(rootPath.resolve("simple-address.yaml").toFile(), rootPath.toString())
DSM dsm = builder.create()


when:
def object = dsm.toObject(rootPath.resolve("simple-address.json").toFile())
//
memoryLog.logAfter()
memoryLog.print()
memoryLog.gc()

then:
assert object instanceof List
List<Map<String, Object>> list = (List<Map<String, Object>>) object
assert 2==list.size()
Map<String, Object> item1 = list.get(0)
def address = item1['address']
assert address instanceof Map
assert address['street']=='Street 1'
assert address['city']=='Istanbul'

}

@Test
void test() throws IOException {

DSMBuilder builder = new DSMBuilder (rootPath.resolve ("SaxParsingHandlerTest.yaml").toFile (), rootPath.toString ())
DSMBuilder builder = new DSMBuilder(rootPath.resolve("SaxParsingHandlerTest.yaml").toFile(), rootPath.toString())

builder.setType (DSMBuilder.TYPE.JSON)
DSM dsm = builder.create ()
Object data = dsm.toObject (rootPath.resolve ("google-merchant-review.json").toFile ())
builder.setType(DSMBuilder.TYPE.JSON)
DSM dsm = builder.create()
Object data = dsm.toObject(rootPath.resolve("google-merchant-review.json").toFile())


StaxParserTest.googleMerchantTEst (data)
StaxParserTest.googleMerchantTEst(data)
}

@Test
void petStoreJaksonJson() throws IOException {
TestMemoryLog memoryLog = new TestMemoryLog ()
memoryLog.logBefore ()
long strat = System.currentTimeMillis ()
TestMemoryLog memoryLog = new TestMemoryLog()
memoryLog.logBefore()
long strat = System.currentTimeMillis()
// for (int i = 0; i < 1; i++) {
ObjectMapper mapper = new ObjectMapper ()
List<Pet> value = mapper.readValue (new File (TestUtils.getTestResourcePath () + pathPlace + "/" + "swagger-pet-store.json"), new TypeReference<List<Pet>> () {
ObjectMapper mapper = new ObjectMapper()
List<Pet> value = mapper.readValue(new File(TestUtils.getTestResourcePath() + pathPlace + "/" + "swagger-pet-store.json"), new TypeReference<List<Pet>>() {
})
// List value = mapper.readValue(new File(TestUtils.getTestResourcePath() +
// pathPlace + "/" + "swagger-pet-store.json"), List.class);
Expand All @@ -77,8 +107,8 @@ class JsonParserSpec extends Specification {
//
// }
// TestUtils.logMemoryUsage();
memoryLog.logAfter ()
memoryLog.print ()
memoryLog.logAfter()
memoryLog.print()
// }
// System.out.println("parse jackson end:" + (System.currentTimeMillis() -
// strat));
Expand Down
15 changes: 15 additions & 0 deletions dsm/src/test/resources/configs/parsing/simple-address.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"address": {
"street": "Street 1",
"city": "Istanbul"
}
},
{
"address": {

"street": "Street 2",
"city": "Ankara"
}
}
]
15 changes: 15 additions & 0 deletions dsm/src/test/resources/configs/parsing/simple-address.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
result:
path: "/"
type: array
fields:
address:
type: object
path: "/address"
fields:
street:
path: "/address/street"
dataType: string
city:
path: "/address/city"
dataType: string

0 comments on commit 5f8b0c0

Please sign in to comment.