Skip to content

Commit

Permalink
Sketch for Generator 2
Browse files Browse the repository at this point in the history
  • Loading branch information
sathishk committed Jan 24, 2024
1 parent eeb8bee commit 9afb4b3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.List;

/**
Expand Down Expand Up @@ -100,7 +101,7 @@ String getJsonType(final Type fieldType) {
return "number";
} else if (fieldType == List.class) {
return "{\"type\":\"array\",\"items\":{\"type\":\"unknown\"}}";
} else if (fieldType.getTypeName().equals("java.math.BigDecimal")) {
} else if (fieldType == BigDecimal.class) {
return "number";
} else {
return "unknown";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.techatpark.sjson.generator;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.reinert.jjschema.v1.JsonSchemaFactory;
import com.github.reinert.jjschema.v1.JsonSchemaV4Factory;
Expand All @@ -20,22 +19,24 @@

public class JsonSchemaGeneratorTest {

private final JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
private final JsonSchemaGenerator jsonSchemaGenerator;

private final ObjectMapper objectMapper;

private final JsonSchemaFactory schemaFactory;

public JsonSchemaGeneratorTest() {
jsonSchemaGenerator = new JsonSchemaGenerator();
objectMapper = new ObjectMapper();
schemaFactory = new JsonSchemaV4Factory();
schemaFactory.setAutoPutDollarSchema(true);
}

@Test
void testGenerator() throws Exception {
JsonSchemaFactory schemaFactory = new JsonSchemaV4Factory();
schemaFactory.setAutoPutDollarSchema(true);
JsonNode productSchema = schemaFactory.createSchema(Product.class);
//System.out.println(productSchema);
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
//System.out.println("\n\n\nYour Output\n================\n");
String generatedSchema = jsonSchemaGenerator.create(Product.class);
//System.out.println(generatedSchema);

ObjectMapper mapper = new ObjectMapper();
JsonNode actualObj = mapper.readTree(generatedSchema);
assertEquals(productSchema, actualObj);
assertEquals(schemaFactory.createSchema(Product.class),
objectMapper.readTree(jsonSchemaGenerator.create(Product.class)),
"JSON Schema was not generated properly");
}

@ParameterizedTest(name = "{index} => fieldType={0}, expectedSchema={1}")
Expand All @@ -55,7 +56,6 @@ private static Stream<Object[]> generateParameterizedTypeSchemaTestData() {
new Object[]{getParameterizedType(List.class, String.class), "{\"type\":\"array\",\"items\":{\"type\":\"string\"}}"},
// Test case 4: Parameterized type with List<BigDecimal> as the actual type argument
new Object[]{getParameterizedType(List.class, BigDecimal.class), "{\"type\":\"array\",\"items\":{\"type\":\"unknown\"}}"}

);
}

Expand Down Expand Up @@ -112,10 +112,8 @@ public Type getOwnerType() {
"java.util.Map, unknown"
})
void testGetJsonType(Class<?> fieldType, String expectedJsonType) {
Type type = fieldType;
String actualJsonType = jsonSchemaGenerator.getJsonType(type);
String actualJsonType = jsonSchemaGenerator.getJsonType(fieldType);
assertEquals(expectedJsonType, actualJsonType);
}


}

0 comments on commit 9afb4b3

Please sign in to comment.