mirror of
https://github.com/mudler/LocalAI.git
synced 2026-01-06 02:29:54 -06:00
fix(grammars): handle empty parameters on object types (#6409)
fix: handle empty parameters on object types Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
committed by
GitHub
parent
5e1d809904
commit
df46a438b8
@@ -171,10 +171,14 @@ func (sc *JSONSchemaConverter) visit(schema map[string]interface{}, name string,
|
||||
}
|
||||
rule := fmt.Sprintf(`"[" space (%s ("," space %s)*)? "]" space`, itemRuleName, itemRuleName)
|
||||
return sc.addRule(ruleName, rule), nil
|
||||
} else if properties, _ := schema["properties"].(map[string]interface{}); (schemaType == "object" || schemaType == "") && len(properties) == 0 {
|
||||
// Handle empty object schema (no properties)
|
||||
rule := `"{" space "}" space`
|
||||
return sc.addRule(ruleName, rule), nil
|
||||
} else {
|
||||
primitiveRule, exists := PRIMITIVE_RULES[schemaType]
|
||||
if !exists {
|
||||
return "", fmt.Errorf("unrecognized schema: %v", schema)
|
||||
return "", fmt.Errorf("unrecognized schema: %v (type: %s)", schema, schemaType)
|
||||
}
|
||||
if ruleName == "root" {
|
||||
schemaType = "root"
|
||||
|
||||
@@ -442,5 +442,39 @@ realvalue
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
It("handles empty object schema without properties", func() {
|
||||
// Test case for the bug fix: schema with empty properties map
|
||||
emptyObjectSchema := `{
|
||||
"type": "object",
|
||||
"properties": {}
|
||||
}`
|
||||
|
||||
grammar, err := NewJSONSchemaConverter("").GrammarFromBytes([]byte(emptyObjectSchema))
|
||||
Expect(err).To(BeNil())
|
||||
Expect(grammar).To(ContainSubstring(`root ::= "{" space "}" space`))
|
||||
})
|
||||
|
||||
It("handles object schema without properties field", func() {
|
||||
// Test case for object schema without properties field at all
|
||||
objectWithoutProperties := `{
|
||||
"type": "object"
|
||||
}`
|
||||
|
||||
grammar, err := NewJSONSchemaConverter("").GrammarFromBytes([]byte(objectWithoutProperties))
|
||||
Expect(err).To(BeNil())
|
||||
Expect(grammar).To(ContainSubstring(`root ::= "{" space "}" space`))
|
||||
})
|
||||
|
||||
It("handles schema with properties but no type field", func() {
|
||||
// Test case for the exact scenario causing the panic: schema with properties but no type
|
||||
schemaWithPropertiesNoType := `{
|
||||
"properties": {}
|
||||
}`
|
||||
|
||||
grammar, err := NewJSONSchemaConverter("").GrammarFromBytes([]byte(schemaWithPropertiesNoType))
|
||||
Expect(err).To(BeNil())
|
||||
Expect(grammar).To(ContainSubstring(`root ::= "{" space "}" space`))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user