Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: fixed handling of additionalProperties to handle the bool/json-schema nature better #180

Merged
merged 3 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 58 additions & 60 deletions datamodel/high/base/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type Schema struct {

// in 3.1 UnevaluatedProperties can be a Schema or a boolean
// https://github.com/pb33f/libopenapi/issues/118
UnevaluatedProperties *DynamicValue[*SchemaProxy, *bool] `json:"unevaluatedProperties,omitempty" yaml:"unevaluatedProperties,omitempty"`
UnevaluatedProperties *DynamicValue[*SchemaProxy, bool] `json:"unevaluatedProperties,omitempty" yaml:"unevaluatedProperties,omitempty"`

// in 3.1 Items can be a Schema or a boolean
Items *DynamicValue[*SchemaProxy, bool] `json:"items,omitempty" yaml:"items,omitempty"`
Expand All @@ -72,35 +72,35 @@ type Schema struct {
Anchor string `json:"$anchor,omitempty" yaml:"$anchor,omitempty"`

// Compatible with all versions
Not *SchemaProxy `json:"not,omitempty" yaml:"not,omitempty"`
Properties map[string]*SchemaProxy `json:"properties,omitempty" yaml:"properties,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
MultipleOf *float64 `json:"multipleOf,omitempty" yaml:"multipleOf,omitempty"`
Maximum *float64 `json:"maximum,omitempty" yaml:"maximum,omitempty"`
Minimum *float64 `json:"minimum,omitempty" yaml:"minimum,omitempty"`
MaxLength *int64 `json:"maxLength,omitempty" yaml:"maxLength,omitempty"`
MinLength *int64 `json:"minLength,omitempty" yaml:"minLength,omitempty"`
Pattern string `json:"pattern,omitempty" yaml:"pattern,omitempty"`
Format string `json:"format,omitempty" yaml:"format,omitempty"`
MaxItems *int64 `json:"maxItems,omitempty" yaml:"maxItems,omitempty"`
MinItems *int64 `json:"minItems,omitempty" yaml:"minItems,omitempty"`
UniqueItems *bool `json:"uniqueItems,omitempty" yaml:"uniqueItems,omitempty"`
MaxProperties *int64 `json:"maxProperties,omitempty" yaml:"maxProperties,omitempty"`
MinProperties *int64 `json:"minProperties,omitempty" yaml:"minProperties,omitempty"`
Required []string `json:"required,omitempty" yaml:"required,omitempty"`
Enum []any `json:"enum,omitempty" yaml:"enum,omitempty"`
AdditionalProperties any `json:"additionalProperties,omitempty" yaml:"additionalProperties,renderZero,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Default any `json:"default,omitempty" yaml:"default,renderZero,omitempty"`
Const any `json:"const,omitempty" yaml:"const,renderZero,omitempty"`
Nullable *bool `json:"nullable,omitempty" yaml:"nullable,omitempty"`
ReadOnly bool `json:"readOnly,omitempty" yaml:"readOnly,omitempty"` // https://github.com/pb33f/libopenapi/issues/30
WriteOnly bool `json:"writeOnly,omitempty" yaml:"writeOnly,omitempty"` // https://github.com/pb33f/libopenapi/issues/30
XML *XML `json:"xml,omitempty" yaml:"xml,omitempty"`
ExternalDocs *ExternalDoc `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`
Example any `json:"example,omitempty" yaml:"example,omitempty"`
Deprecated *bool `json:"deprecated,omitempty" yaml:"deprecated,omitempty"`
Extensions map[string]any `json:"-" yaml:"-"`
Not *SchemaProxy `json:"not,omitempty" yaml:"not,omitempty"`
Properties map[string]*SchemaProxy `json:"properties,omitempty" yaml:"properties,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
MultipleOf *float64 `json:"multipleOf,omitempty" yaml:"multipleOf,omitempty"`
Maximum *float64 `json:"maximum,omitempty" yaml:"maximum,omitempty"`
Minimum *float64 `json:"minimum,omitempty" yaml:"minimum,omitempty"`
MaxLength *int64 `json:"maxLength,omitempty" yaml:"maxLength,omitempty"`
MinLength *int64 `json:"minLength,omitempty" yaml:"minLength,omitempty"`
Pattern string `json:"pattern,omitempty" yaml:"pattern,omitempty"`
Format string `json:"format,omitempty" yaml:"format,omitempty"`
MaxItems *int64 `json:"maxItems,omitempty" yaml:"maxItems,omitempty"`
MinItems *int64 `json:"minItems,omitempty" yaml:"minItems,omitempty"`
UniqueItems *bool `json:"uniqueItems,omitempty" yaml:"uniqueItems,omitempty"`
MaxProperties *int64 `json:"maxProperties,omitempty" yaml:"maxProperties,omitempty"`
MinProperties *int64 `json:"minProperties,omitempty" yaml:"minProperties,omitempty"`
Required []string `json:"required,omitempty" yaml:"required,omitempty"`
Enum []any `json:"enum,omitempty" yaml:"enum,omitempty"`
AdditionalProperties *DynamicValue[*SchemaProxy, bool] `json:"additionalProperties,omitempty" yaml:"additionalProperties,renderZero,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Default any `json:"default,omitempty" yaml:"default,renderZero,omitempty"`
Const any `json:"const,omitempty" yaml:"const,renderZero,omitempty"`
Nullable *bool `json:"nullable,omitempty" yaml:"nullable,omitempty"`
ReadOnly bool `json:"readOnly,omitempty" yaml:"readOnly,omitempty"` // https://github.com/pb33f/libopenapi/issues/30
WriteOnly bool `json:"writeOnly,omitempty" yaml:"writeOnly,omitempty"` // https://github.com/pb33f/libopenapi/issues/30
XML *XML `json:"xml,omitempty" yaml:"xml,omitempty"`
ExternalDocs *ExternalDoc `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`
Example any `json:"example,omitempty" yaml:"example,omitempty"`
Deprecated *bool `json:"deprecated,omitempty" yaml:"deprecated,omitempty"`
Extensions map[string]any `json:"-" yaml:"-"`
low *base.Schema

// Parent Proxy refers back to the low level SchemaProxy that is proxying this schema.
Expand Down Expand Up @@ -211,29 +211,22 @@ func NewSchema(schema *base.Schema) *Schema {
Value: schema.UnevaluatedItems.Value,
})
}
// check if unevaluated properties is a schema
if !schema.UnevaluatedProperties.IsEmpty() && schema.UnevaluatedProperties.Value.IsA() {
s.UnevaluatedProperties = &DynamicValue[*SchemaProxy, *bool]{
A: NewSchemaProxy(
&lowmodel.NodeReference[*base.SchemaProxy]{

var unevaluatedProperties *DynamicValue[*SchemaProxy, bool]
if !schema.UnevaluatedProperties.IsEmpty() {
if schema.UnevaluatedProperties.Value.IsA() {
unevaluatedProperties = &DynamicValue[*SchemaProxy, bool]{
A: NewSchemaProxy(&lowmodel.NodeReference[*base.SchemaProxy]{
ValueNode: schema.UnevaluatedProperties.ValueNode,
Value: schema.UnevaluatedProperties.Value.A,
},
),
N: 0,
}
}

// check if unevaluated properties is a bool
if !schema.UnevaluatedProperties.IsEmpty() && schema.UnevaluatedProperties.Value.IsB() {
s.UnevaluatedProperties = &DynamicValue[*SchemaProxy, *bool]{
B: schema.UnevaluatedProperties.Value.B,
N: 1,
KeyNode: schema.UnevaluatedProperties.KeyNode,
}),
}
} else {
unevaluatedProperties = &DynamicValue[*SchemaProxy, bool]{N: 1, B: schema.UnevaluatedProperties.Value.B}
}
}

if !schema.UnevaluatedProperties.IsEmpty() {
}
s.UnevaluatedProperties = unevaluatedProperties

s.Pattern = schema.Pattern.Value
s.Format = schema.Format.Value
Expand All @@ -248,19 +241,23 @@ func NewSchema(schema *base.Schema) *Schema {
s.Type = append(s.Type, schema.Type.Value.B[i].Value)
}
}
if schema.AdditionalProperties.Value != nil {
if addPropSchema, ok := schema.AdditionalProperties.Value.(*base.SchemaProxy); ok {
s.AdditionalProperties = NewSchemaProxy(&lowmodel.NodeReference[*base.SchemaProxy]{
KeyNode: schema.AdditionalProperties.KeyNode,
ValueNode: schema.AdditionalProperties.ValueNode,
Value: addPropSchema,
})
} else {
// TODO: check for slice and map types and unpack correctly.

s.AdditionalProperties = schema.AdditionalProperties.Value
var additionalProperties *DynamicValue[*SchemaProxy, bool]
if !schema.AdditionalProperties.IsEmpty() {
if schema.AdditionalProperties.Value.IsA() {
additionalProperties = &DynamicValue[*SchemaProxy, bool]{
A: NewSchemaProxy(&lowmodel.NodeReference[*base.SchemaProxy]{
ValueNode: schema.AdditionalProperties.ValueNode,
Value: schema.AdditionalProperties.Value.A,
KeyNode: schema.AdditionalProperties.KeyNode,
}),
}
} else {
additionalProperties = &DynamicValue[*SchemaProxy, bool]{N: 1, B: schema.AdditionalProperties.Value.B}
}
}
s.AdditionalProperties = additionalProperties

s.Description = schema.Description.Value
s.Default = schema.Default.Value
s.Const = schema.Const.Value
Expand Down Expand Up @@ -423,7 +420,8 @@ func NewSchema(schema *base.Schema) *Schema {
Value: schema.Items.Value.A,
KeyNode: schema.Items.KeyNode,
},
)}
),
}
} else {
items = &DynamicValue[*SchemaProxy, bool]{N: 1, B: schema.Items.Value.B}
}
Expand Down
129 changes: 57 additions & 72 deletions datamodel/high/base/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func TestNewSchemaProxyRender(t *testing.T) {
rice:
$ref: '#/components/schemas/rice'`
assert.Equal(t, desired, strings.TrimSpace(string(rend)))

}

func TestNewSchemaProxy_WithObject(t *testing.T) {
Expand Down Expand Up @@ -217,10 +216,7 @@ properties:
type: number
description: a number
example: "2"
additionalProperties:
- chicken
- nugget
- soup
additionalProperties: false
somethingB:
type: object
exclusiveMinimum: true
Expand All @@ -241,8 +237,7 @@ properties:
attribute: true
x-pizza: love
additionalProperties:
why: yes
thatIs: true
type: string
additionalProperties: true
required:
- them
Expand Down Expand Up @@ -315,12 +310,12 @@ $anchor: anchor`
assert.Equal(t, "anchor", compiled.Anchor)

wentLow := compiled.GoLow()
assert.Equal(t, 129, wentLow.AdditionalProperties.ValueNode.Line)
assert.Equal(t, 125, wentLow.AdditionalProperties.ValueNode.Line)
assert.NotNil(t, compiled.GoLowUntyped())

// now render it out!
schemaBytes, _ := compiled.Render()
assert.Len(t, schemaBytes, 3494)
assert.Len(t, schemaBytes, 3417)
}

func TestSchemaObjectWithAllOfSequenceOrder(t *testing.T) {
Expand Down Expand Up @@ -504,7 +499,7 @@ required: [cake, fish]`
assert.Equal(t, float64(334), compiled.Properties["somethingB"].Schema().ExclusiveMaximum.B)
assert.Len(t, compiled.Properties["somethingB"].Schema().Properties["somethingBProp"].Schema().Type, 2)

assert.Equal(t, "nice", compiled.AdditionalProperties.(*SchemaProxy).Schema().Description)
assert.Equal(t, "nice", compiled.AdditionalProperties.A.Schema().Description)

wentLow := compiled.GoLow()
assert.Equal(t, 97, wentLow.AdditionalProperties.ValueNode.Line)
Expand Down Expand Up @@ -556,7 +551,6 @@ func TestSchemaProxy_GoLow(t *testing.T) {
spNil := NewSchemaProxy(nil)
assert.Nil(t, spNil.GoLow())
assert.Nil(t, spNil.GoLowUntyped())

}

func getHighSchema(t *testing.T, yml string) *Schema {
Expand Down Expand Up @@ -836,7 +830,6 @@ allOf:
// now render it out, it should be identical.
schemaBytes, _ := compiled.Render()
assert.Equal(t, testSpec, string(schemaBytes))

}

func TestNewSchemaProxy_RenderSchemaWithMultipleObjectTypes(t *testing.T) {
Expand Down Expand Up @@ -934,8 +927,7 @@ func TestNewSchemaProxy_RenderSchemaEnsurePropertyOrdering(t *testing.T) {
attribute: true
x-pizza: love
additionalProperties:
why: yes
thatIs: true
type: string
additionalProperties: true
xml:
name: XML Thing`
Expand Down Expand Up @@ -989,60 +981,6 @@ func TestNewSchemaProxy_RenderSchemaCheckDiscriminatorMappingOrder(t *testing.T)
assert.Equal(t, testSpec, strings.TrimSpace(string(schemaBytes)))
}

func TestNewSchemaProxy_RenderSchemaCheckAdditionalPropertiesSlice(t *testing.T) {
testSpec := `additionalProperties:
- one
- two
- miss a few
- ninety nine
- hundred`

var compNode yaml.Node
_ = yaml.Unmarshal([]byte(testSpec), &compNode)

sp := new(lowbase.SchemaProxy)
err := sp.Build(nil, compNode.Content[0], nil)
assert.NoError(t, err)

lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
Value: sp,
ValueNode: compNode.Content[0],
}

schemaProxy := NewSchemaProxy(&lowproxy)
compiled := schemaProxy.Schema()

// now render it out, it should be identical.
schemaBytes, _ := compiled.Render()
assert.Len(t, schemaBytes, 91)
}

func TestNewSchemaProxy_RenderSchemaCheckAdditionalPropertiesSliceMap(t *testing.T) {
testSpec := `additionalProperties:
- nice: cake
- yummy: beer
- hot: coffee`

var compNode yaml.Node
_ = yaml.Unmarshal([]byte(testSpec), &compNode)

sp := new(lowbase.SchemaProxy)
err := sp.Build(nil, compNode.Content[0], nil)
assert.NoError(t, err)

lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
Value: sp,
ValueNode: compNode.Content[0],
}

schemaProxy := NewSchemaProxy(&lowproxy)
compiled := schemaProxy.Schema()

// now render it out, it should be identical.
schemaBytes, _ := compiled.Render()
assert.Len(t, schemaBytes, 75)
}

func TestNewSchemaProxy_CheckDefaultBooleanFalse(t *testing.T) {
testSpec := `default: false`

Expand Down Expand Up @@ -1192,8 +1130,7 @@ unevaluatedProperties: true
`
highSchema := getHighSchema(t, yml)

value := true
assert.EqualValues(t, &value, highSchema.UnevaluatedProperties.B)
assert.True(t, highSchema.UnevaluatedProperties.B)
}

func TestUnevaluatedPropertiesBoolean_False(t *testing.T) {
Expand All @@ -1203,6 +1140,54 @@ unevaluatedProperties: false
`
highSchema := getHighSchema(t, yml)

value := false
assert.EqualValues(t, &value, highSchema.UnevaluatedProperties.B)
assert.False(t, highSchema.UnevaluatedProperties.B)
}

func TestUnevaluatedPropertiesBoolean_Unset(t *testing.T) {
yml := `
type: number
`
highSchema := getHighSchema(t, yml)

assert.Nil(t, highSchema.UnevaluatedProperties)
}

func TestAdditionalProperties(t *testing.T) {
testSpec := `type: object
properties:
additionalPropertiesSimpleSchema:
type: object
additionalProperties:
type: string
additionalPropertiesBool:
type: object
additionalProperties: true
additionalPropertiesAnyOf:
type: object
additionalProperties:
anyOf:
- type: string
- type: array
items:
type: string
`

var compNode yaml.Node
_ = yaml.Unmarshal([]byte(testSpec), &compNode)

sp := new(lowbase.SchemaProxy)
err := sp.Build(nil, compNode.Content[0], nil)
assert.NoError(t, err)

lowproxy := low.NodeReference[*lowbase.SchemaProxy]{
Value: sp,
ValueNode: compNode.Content[0],
}

schemaProxy := NewSchemaProxy(&lowproxy)
compiled := schemaProxy.Schema()

assert.Equal(t, []string{"string"}, compiled.Properties["additionalPropertiesSimpleSchema"].Schema().AdditionalProperties.A.Schema().Type)
assert.Equal(t, true, compiled.Properties["additionalPropertiesBool"].Schema().AdditionalProperties.B)
assert.Equal(t, []string{"string"}, compiled.Properties["additionalPropertiesAnyOf"].Schema().AdditionalProperties.A.Schema().AnyOf[0].Schema().Type)
}
Loading