mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-06 12:37:49 +00:00
feat: add attribute to schema model
This commit is contained in:
committed by
Dave Shanley
parent
4c331de207
commit
99bf12c1c7
@@ -5,9 +5,10 @@ package base
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
|
||||||
"github.com/pb33f/libopenapi/datamodel/high"
|
"github.com/pb33f/libopenapi/datamodel/high"
|
||||||
lowmodel "github.com/pb33f/libopenapi/datamodel/low"
|
lowmodel "github.com/pb33f/libopenapi/datamodel/low"
|
||||||
"github.com/pb33f/libopenapi/datamodel/low/base"
|
"github.com/pb33f/libopenapi/datamodel/low/base"
|
||||||
@@ -68,6 +69,9 @@ type Schema struct {
|
|||||||
// in 3.1 Items can be a Schema or a boolean
|
// in 3.1 Items can be a Schema or a boolean
|
||||||
Items *DynamicValue[*SchemaProxy, bool] `json:"items,omitempty" yaml:"items,omitempty"`
|
Items *DynamicValue[*SchemaProxy, bool] `json:"items,omitempty" yaml:"items,omitempty"`
|
||||||
|
|
||||||
|
// 3.1 only, part of the JSON Schema spec provides a way to identify a subschema
|
||||||
|
Anchor string `json:"$anchor,omitempty" yaml:"$anchor,omitempty"`
|
||||||
|
|
||||||
// Compatible with all versions
|
// Compatible with all versions
|
||||||
Not *SchemaProxy `json:"not,omitempty" yaml:"not,omitempty"`
|
Not *SchemaProxy `json:"not,omitempty" yaml:"not,omitempty"`
|
||||||
Properties map[string]*SchemaProxy `json:"properties,omitempty" yaml:"properties,omitempty"`
|
Properties map[string]*SchemaProxy `json:"properties,omitempty" yaml:"properties,omitempty"`
|
||||||
@@ -282,6 +286,10 @@ func NewSchema(schema *base.Schema) *Schema {
|
|||||||
}
|
}
|
||||||
s.Enum = enum
|
s.Enum = enum
|
||||||
|
|
||||||
|
if !schema.Anchor.IsEmpty() {
|
||||||
|
s.Anchor = schema.Anchor.Value
|
||||||
|
}
|
||||||
|
|
||||||
// async work.
|
// async work.
|
||||||
// any polymorphic properties need to be handled in their own threads
|
// any polymorphic properties need to be handled in their own threads
|
||||||
// any properties each need to be processed in their own thread.
|
// any properties each need to be processed in their own thread.
|
||||||
|
|||||||
@@ -266,7 +266,8 @@ minLength: 1
|
|||||||
maxItems: 20
|
maxItems: 20
|
||||||
minItems: 10
|
minItems: 10
|
||||||
maxProperties: 30
|
maxProperties: 30
|
||||||
minProperties: 1`
|
minProperties: 1
|
||||||
|
$anchor: anchor`
|
||||||
|
|
||||||
var compNode yaml.Node
|
var compNode yaml.Node
|
||||||
_ = yaml.Unmarshal([]byte(testSpec), &compNode)
|
_ = yaml.Unmarshal([]byte(testSpec), &compNode)
|
||||||
@@ -310,6 +311,7 @@ minProperties: 1`
|
|||||||
assert.True(t, compiled.WriteOnly)
|
assert.True(t, compiled.WriteOnly)
|
||||||
assert.True(t, *compiled.Deprecated)
|
assert.True(t, *compiled.Deprecated)
|
||||||
assert.True(t, *compiled.Nullable)
|
assert.True(t, *compiled.Nullable)
|
||||||
|
assert.Equal(t, "anchor", compiled.Anchor)
|
||||||
|
|
||||||
wentLow := compiled.GoLow()
|
wentLow := compiled.GoLow()
|
||||||
assert.Equal(t, 129, wentLow.AdditionalProperties.ValueNode.Line)
|
assert.Equal(t, 129, wentLow.AdditionalProperties.ValueNode.Line)
|
||||||
@@ -317,8 +319,7 @@ minProperties: 1`
|
|||||||
|
|
||||||
// now render it out!
|
// now render it out!
|
||||||
schemaBytes, _ := compiled.Render()
|
schemaBytes, _ := compiled.Render()
|
||||||
assert.Len(t, schemaBytes, 3460)
|
assert.Len(t, schemaBytes, 3476)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSchemaObjectWithAllOfSequenceOrder(t *testing.T) {
|
func TestSchemaObjectWithAllOfSequenceOrder(t *testing.T) {
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ const (
|
|||||||
ExclusiveMaximumLabel = "exclusiveMaximum"
|
ExclusiveMaximumLabel = "exclusiveMaximum"
|
||||||
SchemaLabel = "schema"
|
SchemaLabel = "schema"
|
||||||
SchemaTypeLabel = "$schema"
|
SchemaTypeLabel = "$schema"
|
||||||
|
AnchorLabel = "$anchor"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -90,6 +90,7 @@ type Schema struct {
|
|||||||
PropertyNames low.NodeReference[*SchemaProxy]
|
PropertyNames low.NodeReference[*SchemaProxy]
|
||||||
UnevaluatedItems low.NodeReference[*SchemaProxy]
|
UnevaluatedItems low.NodeReference[*SchemaProxy]
|
||||||
UnevaluatedProperties low.NodeReference[*SchemaProxy]
|
UnevaluatedProperties low.NodeReference[*SchemaProxy]
|
||||||
|
Anchor low.NodeReference[string]
|
||||||
|
|
||||||
// Compatible with all versions
|
// Compatible with all versions
|
||||||
Title low.NodeReference[string]
|
Title low.NodeReference[string]
|
||||||
@@ -394,6 +395,9 @@ func (s *Schema) Hash() [32]byte {
|
|||||||
if !s.UnevaluatedItems.IsEmpty() {
|
if !s.UnevaluatedItems.IsEmpty() {
|
||||||
d = append(d, low.GenerateHashString(s.UnevaluatedItems.Value))
|
d = append(d, low.GenerateHashString(s.UnevaluatedItems.Value))
|
||||||
}
|
}
|
||||||
|
if !s.Anchor.IsEmpty() {
|
||||||
|
d = append(d, fmt.Sprint(s.Anchor.Value))
|
||||||
|
}
|
||||||
|
|
||||||
depSchemasKeys := make([]string, len(s.DependentSchemas.Value))
|
depSchemasKeys := make([]string, len(s.DependentSchemas.Value))
|
||||||
z = 0
|
z = 0
|
||||||
@@ -514,6 +518,7 @@ func (s *Schema) GetExtensions() map[low.KeyReference[string]]low.ValueReference
|
|||||||
// - PropertyNames
|
// - PropertyNames
|
||||||
// - UnevaluatedItems
|
// - UnevaluatedItems
|
||||||
// - UnevaluatedProperties
|
// - UnevaluatedProperties
|
||||||
|
// - Anchor
|
||||||
func (s *Schema) Build(root *yaml.Node, idx *index.SpecIndex) error {
|
func (s *Schema) Build(root *yaml.Node, idx *index.SpecIndex) error {
|
||||||
s.Reference = new(low.Reference)
|
s.Reference = new(low.Reference)
|
||||||
if h, _, _ := utils.IsNodeRefValue(root); h {
|
if h, _, _ := utils.IsNodeRefValue(root); h {
|
||||||
@@ -615,6 +620,14 @@ func (s *Schema) Build(root *yaml.Node, idx *index.SpecIndex) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handle anchor if set. (3.1)
|
||||||
|
_, anchorLabel, anchorNode := utils.FindKeyNodeFullTop(AnchorLabel, root.Content)
|
||||||
|
if anchorNode != nil {
|
||||||
|
s.Anchor = low.NodeReference[string]{
|
||||||
|
Value: anchorNode.Value, KeyNode: anchorLabel, ValueNode: anchorLabel,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// handle example if set. (3.0)
|
// handle example if set. (3.0)
|
||||||
_, expLabel, expNode := utils.FindKeyNodeFull(ExampleLabel, root.Content)
|
_, expLabel, expNode := utils.FindKeyNodeFull(ExampleLabel, root.Content)
|
||||||
if expNode != nil {
|
if expNode != nil {
|
||||||
|
|||||||
@@ -150,7 +150,8 @@ examples:
|
|||||||
contains:
|
contains:
|
||||||
type: int
|
type: int
|
||||||
maxContains: 10
|
maxContains: 10
|
||||||
minContains: 1`
|
minContains: 1
|
||||||
|
$anchor: anchor`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Schema(t *testing.T) {
|
func Test_Schema(t *testing.T) {
|
||||||
@@ -308,6 +309,7 @@ func Test_Schema(t *testing.T) {
|
|||||||
assert.Equal(t, "string", sch.PropertyNames.Value.Schema().Type.Value.A)
|
assert.Equal(t, "string", sch.PropertyNames.Value.Schema().Type.Value.A)
|
||||||
assert.Equal(t, "boolean", sch.UnevaluatedItems.Value.Schema().Type.Value.A)
|
assert.Equal(t, "boolean", sch.UnevaluatedItems.Value.Schema().Type.Value.A)
|
||||||
assert.Equal(t, "integer", sch.UnevaluatedProperties.Value.Schema().Type.Value.A)
|
assert.Equal(t, "integer", sch.UnevaluatedProperties.Value.Schema().Type.Value.A)
|
||||||
|
assert.Equal(t, "anchor", sch.Anchor.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSchemaAllOfSequenceOrder(t *testing.T) {
|
func TestSchemaAllOfSequenceOrder(t *testing.T) {
|
||||||
|
|||||||
@@ -136,4 +136,5 @@ const (
|
|||||||
UnevaluatedPropertiesLabel = "unevaluatedProperties"
|
UnevaluatedPropertiesLabel = "unevaluatedProperties"
|
||||||
DependentSchemasLabel = "dependentSchemas"
|
DependentSchemasLabel = "dependentSchemas"
|
||||||
PatternPropertiesLabel = "patternProperties"
|
PatternPropertiesLabel = "patternProperties"
|
||||||
|
AnchorLabel = "$anchor"
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user