Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,12 @@ internal class JsonSchemaValidator private constructor() {
return
}

// Optional enums can generate an anyOf with an explicit null alternative.
if (typeName == TYPE_NULL && depth != ROOT_DEPTH) {
validateSimpleSchema(schema, typeName, path, depth)
return
}

when (typeName) {
TYPE_ARRAY -> validateArraySchema(schema, path, depth)
TYPE_OBJECT -> validateObjectSchema(schema, path, depth)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.node.ObjectNode
import com.openai.errors.OpenAIInvalidDataException
import com.openai.models.responses.ResponseTextConfig
import io.swagger.v3.oas.annotations.media.ArraySchema
import io.swagger.v3.oas.annotations.media.Schema
import java.util.Optional
Expand Down Expand Up @@ -256,6 +257,17 @@ internal class StructuredOutputsTest {
assertThat(validator.isValid()).isTrue
}

@Test
fun schemaTest_optionalEnumIsAcceptedByLocalValidation() {
@Suppress("unused") class X(val value: Optional<MinimalEnum2>)

schema = extractSchema(X::class.java)

assertThatNoException().isThrownBy {
ResponseTextConfig.builder().format(X::class.java).build()
}
}

@Test
fun schemaTest_tinySchemaFromOptionalBoolean() {
@Suppress("unused") class X(val b: Optional<Boolean>)
Expand Down Expand Up @@ -754,6 +766,14 @@ internal class StructuredOutputsTest {
.startsWith("#: 'properties' field is missing, empty or not an object.")
}

@Test
fun schemaTest_nullTypeInRootSchema() {
schema = parseJson("""{"$SCHEMA":"$SCHEMA_VER","type":"null"}""")
validator.validate(schema)

assertThat(validator.errors()).containsExactly("#/type: Unsupported 'type' value: 'null'.")
}

@Test
fun schemaTest_anyOfInRootSchema() {
// OpenAI does not allow `"anyOf"` to appear at the root level of a schema.
Expand Down
Loading