Skip to content

Commit

Permalink
Merge pull request #111 from the-gigi/main
Browse files Browse the repository at this point in the history
Add support for ordered JSON properties in the JSON schema.
  • Loading branch information
sashirestela authored May 1, 2024
2 parents 08fee44 + 92f2d04 commit b7224bd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ private JsonSchemaUtil() {
public static JsonNode classToJsonSchema(Class<?> clazz) {
JsonNode jsonSchema = null;
try {
var jacksonModule = new JacksonModule(JacksonOption.RESPECT_JSONPROPERTY_REQUIRED);
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.sashirestela.openai.support;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -26,6 +27,16 @@ void shouldGenerateEmptyJsonSchemaWhenClassHasNoFields() {
assertEquals(expectedJsonSchema, actualJsonSchema);
}

@Test
void shouldGenerateOrderedJsonSchemaWhenClassHasJsonPropertyOrderAnnotation() {
var actualJsonSchema = JsonSchemaUtil.classToJsonSchema(OrderedTestClass.class).toString();
var expectedJsonSchema = "{\"type\":\"object\",\"properties\":{\"first\":{\"type\":\"string\"}," +
"\"second\":{\"type\":\"integer\"}," +
"\"third\":{\"type\":\"string\"}}," +
"\"required\":[\"first\"]}";
assertEquals(expectedJsonSchema, actualJsonSchema);
}

@NoArgsConstructor
@AllArgsConstructor
@Getter
Expand All @@ -41,4 +52,19 @@ static class TestClass {
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;

}

}

0 comments on commit b7224bd

Please sign in to comment.