Debian Codegraph @

openapi_vocabulary/coercion.go

raw
1 // Copyright 2025 Princess B33f Heavy Industries / Dave Shanley
2 // SPDX-License-Identifier: MIT
3
4 package openapi_vocabulary
5
6 import (
7 "regexp"
8 "strconv"
9
10 "github.com/santhosh-tekuri/jsonschema/v6"
11 )
12
13 // coercionExtension handles Jackson-style scalar coercion (string->boolean/number)
14 type coercionExtension struct {
15 schemaType any // string, []string, or nil
16 allowCoercion bool
17 }
18
19 var (
20 booleanRegex = regexp.MustCompile(`^(true|false)$`)
21 numberRegex = regexp.MustCompile(`^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$`)
22 integerRegex = regexp.MustCompile(`^-?(?:0|[1-9]\d*)$`)
23 )
24
25 func (c *coercionExtension) Validate(ctx *jsonschema.ValidatorContext, v any) {
26 if !c.allowCoercion {
27 return // Coercion disabled - let normal validation handle it
28 }
29
30 str, ok := v.(string)
31 if !ok {
32 return // Not a string - let normal validation handle it
33 }
34
35 // check if we should coerce and validate the string format
36 if c.shouldCoerceToBoolean() {
37 if !c.isValidBooleanString(str) {
38 ctx.AddError(&CoercionError{
39 SourceType: "string",
40 TargetType: "boolean",
41 Value: str,
42 Message: "string value cannot be coerced to boolean, must be 'true' or 'false'",
43 })
44 }
45 return
46 }
47
48 if c.shouldCoerceToNumber() {
49 if !c.isValidNumberString(str) {
50 ctx.AddError(&CoercionError{
51 SourceType: "string",
52 TargetType: "number",
53 Value: str,
54 Message: "string value cannot be coerced to number, must be a valid numeric string",
55 })
56 }
57 return
58 }
59
60 if c.shouldCoerceToInteger() {
61 if !c.isValidIntegerString(str) {
62 ctx.AddError(&CoercionError{
63 SourceType: "string",
64 TargetType: "integer",
65 Value: str,
66 Message: "string value cannot be coerced to integer, must be a valid integer string",
67 })
68 }
69 return
70 }
71 }
72
73 func (c *coercionExtension) shouldCoerceToBoolean() bool {
74 return c.hasType("boolean")
75 }
76
77 func (c *coercionExtension) shouldCoerceToNumber() bool {
78 return c.hasType("number")
79 }
80
81 func (c *coercionExtension) shouldCoerceToInteger() bool {
82 return c.hasType("integer")
83 }
84
85 func (c *coercionExtension) hasType(targetType string) bool {
86 switch t := c.schemaType.(type) {
87 case string:
88 return t == targetType
89 case []any:
90 for _, item := range t {
91 if str, ok := item.(string); ok && str == targetType {
92 return true
93 }
94 }
95 }
96 return false
97 }
98
99 func (c *coercionExtension) isValidBooleanString(s string) bool {
100 return booleanRegex.MatchString(s)
101 }
102
103 func (c *coercionExtension) isValidNumberString(s string) bool {
104 if !numberRegex.MatchString(s) {
105 return false
106 }
107 // Additional validation using strconv
108 _, err := strconv.ParseFloat(s, 64)
109 return err == nil
110 }
111
112 func (c *coercionExtension) isValidIntegerString(s string) bool {
113 if !integerRegex.MatchString(s) {
114 return false
115 }
116 // Additional validation using strconv
117 _, err := strconv.ParseInt(s, 10, 64)
118 return err == nil
119 }
120
121 // CompileCoercion compiles the coercion extension if coercion is allowed and applicable
122 func CompileCoercion(ctx *jsonschema.CompilerContext, obj map[string]any, allowCoercion bool) (jsonschema.SchemaExt, error) {
123 if !allowCoercion {
124 return nil, nil // Coercion disabled
125 }
126
127 // Get the type from the schema
128 schemaType, hasType := obj["type"]
129 if !hasType {
130 return nil, nil // No type specified - no coercion needed
131 }
132
133 // Only apply coercion to scalar types
134 if !IsCoercibleType(schemaType) {
135 return nil, nil
136 }
137
138 return &coercionExtension{
139 schemaType: schemaType,
140 allowCoercion: true,
141 }, nil
142 }
143
144 // IsCoercibleType checks if the schema type is one that supports coercion
145 func IsCoercibleType(schemaType any) bool {
146 switch t := schemaType.(type) {
147 case string:
148 return t == "boolean" || t == "number" || t == "integer"
149 case []any:
150 // for type arrays, check if any coercible type is present
151 for _, item := range t {
152 if str, ok := item.(string); ok {
153 if str == "boolean" || str == "number" || str == "integer" {
154 return true
155 }
156 }
157 }
158 }
159 return false
160 }