Skip to content

Commit 16ce80e

Browse files
chore(jsonschema): add convenience methods for schema and boolean value access (#57)
1 parent c42a384 commit 16ce80e

File tree

11 files changed

+157
-116
lines changed

11 files changed

+157
-116
lines changed

jsonschema/oas3/defs_test.go

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ func TestSchema_Defs_Success(t *testing.T) {
2727

2828
// Navigate to the user property which references #/$defs/User
2929
rootSchema := root.MustGetResolvedSchema()
30-
require.True(t, rootSchema.IsLeft())
30+
require.True(t, rootSchema.IsSchema())
3131

32-
properties := rootSchema.GetLeft().GetProperties()
32+
properties := rootSchema.GetSchema().GetProperties()
3333
require.NotNil(t, properties)
3434

3535
userProperty, exists := properties.Get("user")
@@ -44,10 +44,10 @@ func TestSchema_Defs_Success(t *testing.T) {
4444
// Get the resolved schema
4545
result := userProperty.GetResolvedSchema()
4646
require.NotNil(t, result)
47-
assert.True(t, result.IsLeft())
47+
assert.True(t, result.IsSchema())
4848

4949
// Verify it's the User schema
50-
resolvedSchema := result.GetLeft()
50+
resolvedSchema := result.GetSchema()
5151
assert.Equal(t, []SchemaType{SchemaTypeObject}, resolvedSchema.GetType())
5252

5353
// Verify it has the expected properties
@@ -56,13 +56,13 @@ func TestSchema_Defs_Success(t *testing.T) {
5656

5757
nameProperty, exists := resolvedProperties.Get("name")
5858
require.True(t, exists)
59-
assert.True(t, nameProperty.IsLeft())
60-
assert.Equal(t, []SchemaType{SchemaTypeString}, nameProperty.GetLeft().GetType())
59+
assert.True(t, nameProperty.IsSchema())
60+
assert.Equal(t, []SchemaType{SchemaTypeString}, nameProperty.GetSchema().GetType())
6161

6262
ageProperty, exists := resolvedProperties.Get("age")
6363
require.True(t, exists)
64-
assert.True(t, ageProperty.IsLeft())
65-
assert.Equal(t, []SchemaType{SchemaTypeInteger}, ageProperty.GetLeft().GetType())
64+
assert.True(t, ageProperty.IsSchema())
65+
assert.Equal(t, []SchemaType{SchemaTypeInteger}, ageProperty.GetSchema().GetType())
6666
})
6767

6868
t.Run("resolve chained references through $defs", func(t *testing.T) {
@@ -78,9 +78,9 @@ func TestSchema_Defs_Success(t *testing.T) {
7878

7979
// Navigate to the user property which references User, which itself references Address
8080
rootSchema := root.MustGetResolvedSchema()
81-
require.True(t, rootSchema.IsLeft())
81+
require.True(t, rootSchema.IsSchema())
8282

83-
properties := rootSchema.GetLeft().GetProperties()
83+
properties := rootSchema.GetSchema().GetProperties()
8484
require.NotNil(t, properties)
8585

8686
userProperty, exists := properties.Get("user")
@@ -95,10 +95,10 @@ func TestSchema_Defs_Success(t *testing.T) {
9595
// Get the resolved User schema
9696
userResult := userProperty.GetResolvedSchema()
9797
require.NotNil(t, userResult)
98-
assert.True(t, userResult.IsLeft())
98+
assert.True(t, userResult.IsSchema())
9999

100100
// Verify the User schema has an address property that references Address
101-
userSchema := userResult.GetLeft()
101+
userSchema := userResult.GetSchema()
102102
userProperties := userSchema.GetProperties()
103103
require.NotNil(t, userProperties)
104104

@@ -123,9 +123,9 @@ func TestSchema_Defs_Success(t *testing.T) {
123123

124124
// Navigate to the chainedRef property which references ChainedRef -> ChainedTarget
125125
rootSchema := root.MustGetResolvedSchema()
126-
require.True(t, rootSchema.IsLeft())
126+
require.True(t, rootSchema.IsSchema())
127127

128-
properties := rootSchema.GetLeft().GetProperties()
128+
properties := rootSchema.GetSchema().GetProperties()
129129
require.NotNil(t, properties)
130130

131131
chainedProperty, exists := properties.Get("chainedRef")
@@ -140,10 +140,10 @@ func TestSchema_Defs_Success(t *testing.T) {
140140
// Get the resolved schema - should be the final ChainedTarget
141141
result := chainedProperty.GetResolvedSchema()
142142
require.NotNil(t, result)
143-
assert.True(t, result.IsLeft())
143+
assert.True(t, result.IsSchema())
144144

145145
// Verify it's the ChainedTarget schema
146-
resolvedSchema := result.GetLeft()
146+
resolvedSchema := result.GetSchema()
147147
assert.Equal(t, []SchemaType{SchemaTypeObject}, resolvedSchema.GetType())
148148

149149
// Verify it has the expected properties
@@ -152,13 +152,13 @@ func TestSchema_Defs_Success(t *testing.T) {
152152

153153
valueProperty, exists := resolvedProperties.Get("value")
154154
require.True(t, exists)
155-
assert.True(t, valueProperty.IsLeft())
156-
assert.Equal(t, []SchemaType{SchemaTypeString}, valueProperty.GetLeft().GetType())
155+
assert.True(t, valueProperty.IsSchema())
156+
assert.Equal(t, []SchemaType{SchemaTypeString}, valueProperty.GetSchema().GetType())
157157

158158
descProperty, exists := resolvedProperties.Get("description")
159159
require.True(t, exists)
160-
assert.True(t, descProperty.IsLeft())
161-
assert.Equal(t, []SchemaType{SchemaTypeString}, descProperty.GetLeft().GetType())
160+
assert.True(t, descProperty.IsSchema())
161+
assert.Equal(t, []SchemaType{SchemaTypeString}, descProperty.GetSchema().GetType())
162162
})
163163

164164
t.Run("resolve reference from within nested schema with local $defs", func(t *testing.T) {
@@ -174,16 +174,16 @@ func TestSchema_Defs_Success(t *testing.T) {
174174

175175
// Navigate to the NestedSchema which has its own $defs
176176
rootSchema := root.MustGetResolvedSchema()
177-
require.True(t, rootSchema.IsLeft())
177+
require.True(t, rootSchema.IsSchema())
178178

179-
nestedSchema, ok := rootSchema.GetLeft().GetDefs().Get("NestedSchema")
179+
nestedSchema, ok := rootSchema.GetSchema().GetDefs().Get("NestedSchema")
180180
require.True(t, ok)
181181

182182
nestedSchemaResolved := nestedSchema.MustGetResolvedSchema()
183-
require.True(t, nestedSchemaResolved.IsLeft())
183+
require.True(t, nestedSchemaResolved.IsSchema())
184184

185185
// Get the localRef property which should reference the local $defs/LocalDef
186-
properties := nestedSchemaResolved.GetLeft().GetProperties()
186+
properties := nestedSchemaResolved.GetSchema().GetProperties()
187187
require.NotNil(t, properties)
188188

189189
localRef, exists := properties.Get("localRef")
@@ -199,10 +199,10 @@ func TestSchema_Defs_Success(t *testing.T) {
199199
// Get the resolved localRef schema
200200
localRefResolved := localRef.GetResolvedSchema()
201201
require.NotNil(t, localRefResolved)
202-
require.True(t, localRefResolved.IsLeft())
202+
require.True(t, localRefResolved.IsSchema())
203203

204204
// Verify it's the LocalDef schema
205-
resolvedSchema := localRefResolved.GetLeft()
205+
resolvedSchema := localRefResolved.GetSchema()
206206
assert.Equal(t, []SchemaType{SchemaTypeObject}, resolvedSchema.GetType())
207207

208208
// Verify it has the expected properties
@@ -211,8 +211,8 @@ func TestSchema_Defs_Success(t *testing.T) {
211211

212212
localValueProperty, exists := resolvedProperties.Get("localValue")
213213
require.True(t, exists)
214-
assert.True(t, localValueProperty.IsLeft())
215-
assert.Equal(t, []SchemaType{SchemaTypeString}, localValueProperty.GetLeft().GetType())
214+
assert.True(t, localValueProperty.IsSchema())
215+
assert.Equal(t, []SchemaType{SchemaTypeString}, localValueProperty.GetSchema().GetType())
216216
})
217217

218218
t.Run("$defs getter method works correctly", func(t *testing.T) {
@@ -222,26 +222,26 @@ func TestSchema_Defs_Success(t *testing.T) {
222222
require.NoError(t, err)
223223

224224
// Get the $defs from the root schema
225-
require.True(t, root.IsLeft())
226-
schema := root.GetLeft()
225+
require.True(t, root.IsSchema())
226+
schema := root.GetSchema()
227227
defs := schema.GetDefs()
228228
require.NotNil(t, defs)
229229

230230
// Verify we have the expected definitions
231231
userDef, exists := defs.Get("User")
232232
require.True(t, exists)
233-
assert.True(t, userDef.IsLeft())
234-
assert.Equal(t, []SchemaType{SchemaTypeObject}, userDef.GetLeft().GetType())
233+
assert.True(t, userDef.IsSchema())
234+
assert.Equal(t, []SchemaType{SchemaTypeObject}, userDef.GetSchema().GetType())
235235

236236
addressDef, exists := defs.Get("Address")
237237
require.True(t, exists)
238-
assert.True(t, addressDef.IsLeft())
239-
assert.Equal(t, []SchemaType{SchemaTypeObject}, addressDef.GetLeft().GetType())
238+
assert.True(t, addressDef.IsSchema())
239+
assert.Equal(t, []SchemaType{SchemaTypeObject}, addressDef.GetSchema().GetType())
240240

241241
nestedDef, exists := defs.Get("NestedSchema")
242242
require.True(t, exists)
243-
assert.True(t, nestedDef.IsLeft())
244-
assert.Equal(t, []SchemaType{SchemaTypeObject}, nestedDef.GetLeft().GetType())
243+
assert.True(t, nestedDef.IsSchema())
244+
assert.Equal(t, []SchemaType{SchemaTypeObject}, nestedDef.GetSchema().GetType())
245245
})
246246
}
247247

@@ -261,9 +261,9 @@ func TestSchema_Defs_Error(t *testing.T) {
261261

262262
// Navigate to the nonExistentRef property which references a non-existent definition
263263
rootSchema := root.MustGetResolvedSchema()
264-
require.True(t, rootSchema.IsLeft())
264+
require.True(t, rootSchema.IsSchema())
265265

266-
properties := rootSchema.GetLeft().GetProperties()
266+
properties := rootSchema.GetSchema().GetProperties()
267267
require.NotNil(t, properties)
268268

269269
nonExistentProperty, exists := properties.Get("nonExistentRef")
@@ -358,9 +358,9 @@ func TestSchema_ExternalDefs_Success(t *testing.T) {
358358

359359
// Navigate to the externalUser property which references external $defs
360360
rootSchema := testSchema.MustGetResolvedSchema()
361-
require.True(t, rootSchema.IsLeft())
361+
require.True(t, rootSchema.IsSchema())
362362

363-
properties := rootSchema.GetLeft().GetProperties()
363+
properties := rootSchema.GetSchema().GetProperties()
364364
require.NotNil(t, properties)
365365

366366
externalUserProperty, exists := properties.Get("externalUser")
@@ -375,10 +375,10 @@ func TestSchema_ExternalDefs_Success(t *testing.T) {
375375
// Get the resolved schema
376376
result := externalUserProperty.GetResolvedSchema()
377377
require.NotNil(t, result, "resolved schema should not be nil")
378-
assert.True(t, result.IsLeft(), "resolved schema should be a schema, not a reference")
378+
assert.True(t, result.IsSchema(), "resolved schema should be a schema, not a reference")
379379

380380
// Verify the resolved schema has the expected structure
381-
resolvedSchema := result.GetLeft()
381+
resolvedSchema := result.GetSchema()
382382
assert.Equal(t, []SchemaType{SchemaTypeObject}, resolvedSchema.GetType(), "resolved schema should be object type")
383383

384384
resolvedProperties := resolvedSchema.GetProperties()
@@ -387,13 +387,13 @@ func TestSchema_ExternalDefs_Success(t *testing.T) {
387387
// Verify ExternalUser properties
388388
idProperty, exists := resolvedProperties.Get("id")
389389
require.True(t, exists, "resolved schema should have id property")
390-
assert.True(t, idProperty.IsLeft(), "id property should be a schema")
391-
assert.Equal(t, []SchemaType{SchemaTypeInteger}, idProperty.GetLeft().GetType(), "id should be integer type")
390+
assert.True(t, idProperty.IsSchema(), "id property should be a schema")
391+
assert.Equal(t, []SchemaType{SchemaTypeInteger}, idProperty.GetSchema().GetType(), "id should be integer type")
392392

393393
nameProperty, exists := resolvedProperties.Get("name")
394394
require.True(t, exists, "resolved schema should have name property")
395-
assert.True(t, nameProperty.IsLeft(), "name property should be a schema")
396-
assert.Equal(t, []SchemaType{SchemaTypeString}, nameProperty.GetLeft().GetType(), "name should be string type")
395+
assert.True(t, nameProperty.IsSchema(), "name property should be a schema")
396+
assert.Equal(t, []SchemaType{SchemaTypeString}, nameProperty.GetSchema().GetType(), "name should be string type")
397397
})
398398

399399
t.Run("resolve reference to non-existent external $defs", func(t *testing.T) {
@@ -422,9 +422,9 @@ func TestSchema_ExternalDefs_Success(t *testing.T) {
422422

423423
// Navigate to the nonExistentUser property which references non-existent external $defs
424424
rootSchema := testSchema.MustGetResolvedSchema()
425-
require.True(t, rootSchema.IsLeft())
425+
require.True(t, rootSchema.IsSchema())
426426

427-
properties := rootSchema.GetLeft().GetProperties()
427+
properties := rootSchema.GetSchema().GetProperties()
428428
require.NotNil(t, properties)
429429

430430
nonExistentProperty, exists := properties.Get("nonExistentUser")

jsonschema/oas3/inline.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func analyzeReferences(ctx context.Context, schema *JSONSchema[Referenceable], o
293293
return analyzeReferences(ctx, ConcreteToReferenceable(resolved), opts, refTracker, visited, counter)
294294
}
295295

296-
if resolved.IsRight() {
296+
if resolved.IsBool() {
297297
return nil // Boolean schemas don't have references to analyze
298298
}
299299

@@ -302,7 +302,7 @@ func analyzeReferences(ctx context.Context, schema *JSONSchema[Referenceable], o
302302
currentFrame = visited[len(visited)-1]
303303
}
304304

305-
js := resolved.GetLeft()
305+
js := resolved.GetSchema()
306306

307307
// Analyze all nested schemas
308308
for _, schema := range js.AllOf {
@@ -445,7 +445,7 @@ func inlineRecursive(ctx context.Context, schema *JSONSchema[Referenceable], opt
445445
// This is the second+ occurrence of a circular reference
446446
// Rewrite the reference if needed, then don't recurse
447447
if info.rewrittenRef != "" {
448-
schema.GetLeft().Ref = pointer.From(references.Reference(info.rewrittenRef))
448+
schema.GetSchema().Ref = pointer.From(references.Reference(info.rewrittenRef))
449449
rewrittenAbsRef := references.Reference(opts.ResolveOptions.TargetLocation + info.rewrittenRef)
450450
// Add reverse lookup for the rewritten reference
451451
if !refTracker.Has(rewrittenAbsRef.String()) {
@@ -459,12 +459,12 @@ func inlineRecursive(ctx context.Context, schema *JSONSchema[Referenceable], opt
459459
}
460460
// If not preserve, this reference should be inlined - we'll process its content below
461461
}
462-
if resolved.IsRight() {
462+
if resolved.IsBool() {
463463
inlineSchemaInPlace(schema, resolved)
464464
return schema, nil
465465
}
466466

467-
js := resolved.GetLeft()
467+
js := resolved.GetSchema()
468468

469469
// Walk through allOf schemas
470470
for i, s := range js.AllOf {
@@ -613,7 +613,7 @@ func inlineRecursive(ctx context.Context, schema *JSONSchema[Referenceable], opt
613613
inlineSchemaInPlace(schema, resolved)
614614
} else if info.rewrittenRef != "" {
615615
// This is a preserved reference - rewrite it to point to the new $defs location
616-
schema.GetLeft().Ref = pointer.From(references.Reference(info.rewrittenRef))
616+
schema.GetSchema().Ref = pointer.From(references.Reference(info.rewrittenRef))
617617
rewrittenAbsRef := references.Reference(opts.ResolveOptions.TargetLocation + info.rewrittenRef)
618618
// Add reverse lookup for the rewritten reference
619619
if !refTracker.Has(rewrittenAbsRef.String()) {
@@ -662,11 +662,11 @@ func inlineSchemaInPlace(schema *JSONSchema[Referenceable], resolved *JSONSchema
662662

663663
// removeUnusedDefs removes $defs that are no longer referenced after inlining
664664
func removeUnusedDefs(_ context.Context, schema *JSONSchema[Referenceable], refTracker *sequencedmap.Map[string, *refInfo]) {
665-
if schema == nil || !schema.IsLeft() {
665+
if schema == nil || !schema.IsSchema() {
666666
return
667667
}
668668

669-
schemaObj := schema.GetLeft()
669+
schemaObj := schema.GetSchema()
670670
if schemaObj == nil || schemaObj.Defs == nil || schemaObj.Defs.Len() == 0 {
671671
return
672672
}
@@ -802,11 +802,11 @@ func consolidateDefinitions(schema *JSONSchema[Referenceable], refTracker *seque
802802
}
803803

804804
// Ensure we have a schema object (not a boolean schema)
805-
if schema.IsRight() {
805+
if schema.IsBool() {
806806
return errors.New("cannot add definitions to a boolean schema")
807807
}
808808

809-
js := schema.GetLeft()
809+
js := schema.GetSchema()
810810
if js == nil {
811811
return errors.New("schema object is nil")
812812
}

0 commit comments

Comments
 (0)