-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added SchemaConverter interface for controlling Schema generation fro…
…m classes
- Loading branch information
1 parent
5db4c2c
commit 13a9a3a
Showing
7 changed files
with
181 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/io/github/sashirestela/openai/common/function/SchemaConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.github.sashirestela.openai.common.function; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
public interface SchemaConverter { | ||
|
||
JsonNode convert(Class<?> c); | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/io/github/sashirestela/openai/support/DefaultSchemaConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.github.sashirestela.openai.support; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.github.victools.jsonschema.generator.*; | ||
import com.github.victools.jsonschema.module.jackson.JacksonModule; | ||
import com.github.victools.jsonschema.module.jackson.JacksonOption; | ||
import io.github.sashirestela.openai.SimpleUncheckedException; | ||
import io.github.sashirestela.openai.common.function.SchemaConverter; | ||
|
||
import static io.github.sashirestela.openai.support.JsonSchemaUtil.JSON_EMPTY_CLASS; | ||
|
||
public class DefaultSchemaConverter implements SchemaConverter { | ||
private final SchemaGenerator schemaGenerator; | ||
private final ObjectMapper objectMapper; | ||
|
||
public DefaultSchemaConverter() { | ||
objectMapper = new ObjectMapper(); | ||
var jacksonModule = new JacksonModule(JacksonOption.RESPECT_JSONPROPERTY_REQUIRED, | ||
JacksonOption.RESPECT_JSONPROPERTY_ORDER); | ||
var configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, | ||
OptionPreset.PLAIN_JSON) | ||
.with(jacksonModule) | ||
.without(Option.SCHEMA_VERSION_INDICATOR); | ||
var config = configBuilder.build(); | ||
schemaGenerator = new SchemaGenerator(config); | ||
} | ||
|
||
@Override | ||
public JsonNode convert(Class<?> clazz) { | ||
JsonNode jsonSchema; | ||
try { | ||
jsonSchema = schemaGenerator.generateSchema(clazz); | ||
if (jsonSchema.get("properties") == null) { | ||
jsonSchema = objectMapper.readTree(JSON_EMPTY_CLASS); | ||
} | ||
|
||
} catch (Exception e) { | ||
throw new SimpleUncheckedException("Cannot generate the Json Schema for the class {0}.", clazz.getName(), e); | ||
} | ||
return jsonSchema; | ||
} | ||
} |
34 changes: 4 additions & 30 deletions
34
src/main/java/io/github/sashirestela/openai/support/JsonSchemaUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,19 @@ | ||
package io.github.sashirestela.openai.support; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.github.victools.jsonschema.generator.Option; | ||
import com.github.victools.jsonschema.generator.OptionPreset; | ||
import com.github.victools.jsonschema.generator.SchemaGenerator; | ||
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder; | ||
import com.github.victools.jsonschema.generator.SchemaVersion; | ||
import com.github.victools.jsonschema.module.jackson.JacksonModule; | ||
import com.github.victools.jsonschema.module.jackson.JacksonOption; | ||
import io.github.sashirestela.openai.SimpleUncheckedException; | ||
import io.github.sashirestela.openai.common.function.SchemaConverter; | ||
|
||
public class JsonSchemaUtil { | ||
|
||
public static final SchemaConverter defaultConverter= new DefaultSchemaConverter(); | ||
|
||
public static final String JSON_EMPTY_CLASS = "{\"type\":\"object\",\"properties\":{}}"; | ||
private static ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
private JsonSchemaUtil() { | ||
} | ||
|
||
public static JsonNode classToJsonSchema(Class<?> clazz) { | ||
JsonNode jsonSchema = null; | ||
try { | ||
var jacksonModule = new JacksonModule(JacksonOption.RESPECT_JSONPROPERTY_REQUIRED, | ||
JacksonOption.RESPECT_JSONPROPERTY_ORDER); | ||
var configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, | ||
OptionPreset.PLAIN_JSON) | ||
.with(jacksonModule) | ||
.without(Option.SCHEMA_VERSION_INDICATOR); | ||
var config = configBuilder.build(); | ||
var generator = new SchemaGenerator(config); | ||
jsonSchema = generator.generateSchema(clazz); | ||
if (jsonSchema.get("properties") == null) { | ||
jsonSchema = objectMapper.readTree(JSON_EMPTY_CLASS); | ||
} | ||
|
||
} catch (Exception e) { | ||
throw new SimpleUncheckedException("Cannot generate the Json Schema for the class {0}.", | ||
clazz.getName(), e); | ||
} | ||
return jsonSchema; | ||
return defaultConverter.convert(clazz); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
src/test/java/io/github/sashirestela/openai/support/CustomSchemaConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.github.sashirestela.openai.support; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.github.victools.jsonschema.generator.*; | ||
import com.github.victools.jsonschema.generator.Module; | ||
import com.github.victools.jsonschema.module.jackson.JacksonModule; | ||
import com.github.victools.jsonschema.module.jackson.JacksonOption; | ||
import io.github.sashirestela.openai.SimpleUncheckedException; | ||
import io.github.sashirestela.openai.common.function.SchemaConverter; | ||
|
||
public class CustomSchemaConverter implements SchemaConverter { | ||
private final SchemaGenerator schemaGenerator; | ||
private final ObjectMapper objectMapper; | ||
public static final String JSON_EMPTY_CLASS = "{\"type\":\"object\",\"properties\":{}}"; | ||
|
||
public CustomSchemaConverter() { | ||
objectMapper = new ObjectMapper(); | ||
var jacksonModule = new JacksonModule(JacksonOption.RESPECT_JSONPROPERTY_REQUIRED, | ||
JacksonOption.RESPECT_JSONPROPERTY_ORDER); | ||
var configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, | ||
OptionPreset.PLAIN_JSON) | ||
.with(jacksonModule) | ||
.with(builder -> builder.forTypesInGeneral().withTypeAttributeOverride( | ||
(collectedTypeAttributes, scope, context) -> collectedTypeAttributes.put("myCustomProperty",true))) | ||
.without(Option.SCHEMA_VERSION_INDICATOR); | ||
var config = configBuilder.build(); | ||
schemaGenerator = new SchemaGenerator(config); | ||
} | ||
|
||
@Override | ||
public JsonNode convert(Class<?> clazz) { | ||
JsonNode jsonSchema; | ||
try { | ||
jsonSchema = schemaGenerator.generateSchema(clazz); | ||
if (jsonSchema.get("properties") == null) { | ||
jsonSchema = objectMapper.readTree(JSON_EMPTY_CLASS); | ||
} | ||
|
||
} catch (Exception e) { | ||
throw new SimpleUncheckedException("Cannot generate the Json Schema for the class {0}.", clazz.getName(), e); | ||
} | ||
return jsonSchema; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/test/java/io/github/sashirestela/openai/support/CustomSchemaConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package io.github.sashirestela.openai.support; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonPropertyOrder; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import io.github.sashirestela.openai.common.function.SchemaConverter; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static io.github.sashirestela.openai.support.JsonSchemaUtil.JSON_EMPTY_CLASS; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class CustomSchemaConverterTest { | ||
|
||
private static SchemaConverter schemaConverter=new CustomSchemaConverter(); | ||
|
||
|
||
@Test | ||
void shouldGenerateFullJsonSchemaWhenClassHasSomeFields() { | ||
var actualJsonSchema = schemaConverter.convert(TestClass.class).toString(); | ||
var expectedJsonSchema = "{\"type\":\"object\",\"properties\":{\"first\":{\"type\":\"string\",\"myCustomProperty\":true},\"second\":{\"type\":\"integer\",\"myCustomProperty\":true}},\"required\":[\"first\"],\"myCustomProperty\":true}"; | ||
assertEquals(expectedJsonSchema, actualJsonSchema); | ||
} | ||
|
||
@Test | ||
void shouldGenerateEmptyJsonSchemaWhenClassHasNoFields() { | ||
var actualJsonSchema = schemaConverter.convert(EmptyClass.class).toString(); | ||
var expectedJsonSchema = JSON_EMPTY_CLASS; | ||
assertEquals(expectedJsonSchema, actualJsonSchema); | ||
} | ||
|
||
@Test | ||
void shouldGenerateOrderedJsonSchemaWhenClassHasJsonPropertyOrderAnnotation() { | ||
var actualJsonSchema = schemaConverter.convert(OrderedTestClass.class).toString(); | ||
var expectedJsonSchema = "{\"type\":\"object\",\"properties\":{\"first\":{\"type\":\"string\",\"myCustomProperty\":true},\"second\":{\"type\":\"integer\",\"myCustomProperty\":true},\"third\":{\"type\":\"string\",\"myCustomProperty\":true}},\"required\":[\"first\"],\"myCustomProperty\":true}"; | ||
assertEquals(expectedJsonSchema, actualJsonSchema); | ||
} | ||
|
||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Getter | ||
static class TestClass { | ||
|
||
@JsonProperty(required = true) | ||
public String first; | ||
|
||
public Integer second; | ||
|
||
} | ||
|
||
static class EmptyClass { | ||
} | ||
|
||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Getter | ||
@JsonPropertyOrder({ "first", "second", "third" }) | ||
static class OrderedTestClass { | ||
|
||
@JsonProperty(required = true) | ||
public String first; | ||
|
||
public Integer second; | ||
|
||
public String third; | ||
|
||
} | ||
|
||
|
||
|
||
} |