Fix ref object parsing

This commit is contained in:
Dmitry
2023-01-12 17:21:26 +01:00
committed by Dave Shanley
parent d73fde2e42
commit cc5b7e73d9
4 changed files with 87 additions and 4 deletions

View File

@@ -482,6 +482,11 @@ func (s *Schema) Build(root *yaml.Node, idx *index.SpecIndex) error {
}
}
// Build model using possibly dereferenced root
if err := low.BuildModel(root, s); err != nil {
return err
}
s.extractExtensions(root)
// determine schema type, singular (3.0) or multiple (3.1), use a variable value

View File

@@ -5,7 +5,7 @@ package base
import (
"crypto/sha256"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/utils"
"gopkg.in/yaml.v3"
@@ -35,8 +35,8 @@ import (
// and slow things down when building. By preventing recursion through every polymorphic item, building models is kept
// fast and snappy, which is desired for realtime processing of specs.
//
// - Q: Yeah, but, why not just use state to avoiding re-visiting seen polymorphic nodes?
// - A: It's slow, takes up memory and still has runaway potential in very, very long chains.
// - Q: Yeah, but, why not just use state to avoiding re-visiting seen polymorphic nodes?
// - A: It's slow, takes up memory and still has runaway potential in very, very long chains.
//
// 3. Short Circuit Errors.
//
@@ -80,7 +80,6 @@ func (sp *SchemaProxy) Schema() *Schema {
return sp.rendered
}
schema := new(Schema)
_ = low.BuildModel(sp.vn, schema)
err := schema.Build(sp.vn, sp.idx)
if err != nil {
sp.buildError = err

View File

@@ -722,3 +722,45 @@ components:
//parameterOne 'x-custom-burgers' (some burgers) has 2 burgers, 'anotherBurger' has mayo sauce and a lamb patty
}
func TestSchemaRefIsFollowed(t *testing.T) {
petstore, _ := ioutil.ReadFile("test_specs/ref-followed.yaml")
// create a new document from specification bytes
document, err := NewDocument(petstore)
// if anything went wrong, an error is thrown
if err != nil {
panic(fmt.Sprintf("cannot create new document: %e", err))
}
// because we know this is a v3 spec, we can build a ready to go model from it.
v3Model, errors := document.BuildV3Model()
// if anything went wrong when building the v3 model, a slice of errors will be returned
if len(errors) > 0 {
for i := range errors {
fmt.Printf("error: %e\n", errors[i])
}
panic(fmt.Sprintf("cannot create v3 model from document: %d errors reported", len(errors)))
}
// get a count of the number of paths and schemas.
schemas := v3Model.Model.Components.Schemas
assert.Equal(t, 4, len(schemas))
fp := schemas["FP"]
fbsref := schemas["FBSRef"]
assert.Equal(t, fp.Schema().Pattern, fbsref.Schema().Pattern)
assert.Equal(t, fp.Schema().Example, fbsref.Schema().Example)
byte := schemas["Byte"]
uint64 := schemas["UInt64"]
assert.Equal(t, uint64.Schema().Format, byte.Schema().Format)
assert.Equal(t, uint64.Schema().Type, byte.Schema().Type)
assert.Equal(t, uint64.Schema().Nullable, byte.Schema().Nullable)
assert.Equal(t, uint64.Schema().Example, byte.Schema().Example)
assert.Equal(t, uint64.Schema().Minimum, byte.Schema().Minimum)
}

View File

@@ -0,0 +1,37 @@
openapi: 3.1.0
info:
title: All scalar types
version: 1.0.0
description: These types used in testing
servers:
- url: https://api.server.test/v1
paths:
/test:
get:
operationId: 20CBF3CA-4F9F-455E-8A3E-3C2B2CD9849A
responses:
"200":
type: string
description: This is my schema that is great!
components:
schemas:
FBSRef:
$ref: "#/components/schemas/FP"
FP:
type: string
description: Always use full F{
example: asd asd asd
pattern: '^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[\+-]\d{2}:\d{2})$'
UInt64:
type: integer
format: uint64
nullable: true
example: 1
minimum: 1
Byte:
$ref: "#/components/schemas/UInt64"