-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathschema_hash_keywords_test.go
More file actions
108 lines (100 loc) · 2.86 KB
/
Copy pathschema_hash_keywords_test.go
File metadata and controls
108 lines (100 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2026 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package libopenapi
import (
"testing"
"github.com/pb33f/testify/assert"
"github.com/pb33f/testify/require"
)
const schemaHashKeywordsSpec = `openapi: 3.1.0
info:
title: schema hash keywords
version: 1.0.0
paths:
/min:
post:
requestBody:
content:
application/json:
schema:
type: integer
minimum: 5
/max:
post:
requestBody:
content:
application/json:
schema:
type: integer
maximum: 5
/one-of:
post:
requestBody:
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/Widget'
- type: object
/all-of:
post:
requestBody:
content:
application/json:
schema:
allOf:
- $ref: '#/components/schemas/Widget'
- type: object
/widget:
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Widget'
/another-widget:
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Widget'
/inline-widget:
post:
requestBody:
content:
application/json:
schema:
type: object
properties:
id:
type: string
components:
schemas:
Widget:
type: object
properties:
id:
type: string
`
// libopenapi-validator caches every compiled schema under schema.GoLow().Hash(). While the hash ignored which
// keyword held a value, /max was validated with the schema compiled for /min, and an allOf body with the schema
// compiled for a oneOf over the same members. The same schema, reached directly or through a $ref, must still share
// one hash so the cache keeps working.
func TestSchemaHashDistinguishesKeywordsAcrossOperations(t *testing.T) {
doc, err := NewDocument([]byte(schemaHashKeywordsSpec))
require.NoError(t, err)
model, errs := doc.BuildV3Model()
require.Nil(t, errs)
bodySchemaHash := func(path string) uint64 {
item, ok := model.Model.Paths.PathItems.Get(path)
require.Truef(t, ok, "path %s missing", path)
mediaType, ok := item.Post.RequestBody.Content.Get("application/json")
require.Truef(t, ok, "path %s has no application/json body", path)
return mediaType.Schema.Schema().GoLow().Hash()
}
assert.NotEqual(t, bodySchemaHash("/min"), bodySchemaHash("/max"))
assert.NotEqual(t, bodySchemaHash("/one-of"), bodySchemaHash("/all-of"))
assert.Equal(t, bodySchemaHash("/widget"), bodySchemaHash("/another-widget"))
assert.Equal(t, bodySchemaHash("/widget"), bodySchemaHash("/inline-widget"))
}