mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-09 12:37:49 +00:00
Fix ref object parsing
This commit is contained in:
@@ -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)
|
s.extractExtensions(root)
|
||||||
|
|
||||||
// determine schema type, singular (3.0) or multiple (3.1), use a variable value
|
// determine schema type, singular (3.0) or multiple (3.1), use a variable value
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ package base
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"github.com/pb33f/libopenapi/datamodel/low"
|
|
||||||
"github.com/pb33f/libopenapi/index"
|
"github.com/pb33f/libopenapi/index"
|
||||||
"github.com/pb33f/libopenapi/utils"
|
"github.com/pb33f/libopenapi/utils"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
@@ -80,7 +80,6 @@ func (sp *SchemaProxy) Schema() *Schema {
|
|||||||
return sp.rendered
|
return sp.rendered
|
||||||
}
|
}
|
||||||
schema := new(Schema)
|
schema := new(Schema)
|
||||||
_ = low.BuildModel(sp.vn, schema)
|
|
||||||
err := schema.Build(sp.vn, sp.idx)
|
err := schema.Build(sp.vn, sp.idx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sp.buildError = err
|
sp.buildError = err
|
||||||
|
|||||||
@@ -722,3 +722,45 @@ components:
|
|||||||
//parameterOne 'x-custom-burgers' (some burgers) has 2 burgers, 'anotherBurger' has mayo sauce and a lamb patty
|
//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)
|
||||||
|
}
|
||||||
|
|||||||
37
test_specs/ref-followed.yaml
Normal file
37
test_specs/ref-followed.yaml
Normal 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"
|
||||||
Reference in New Issue
Block a user