mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-06 12:37:49 +00:00
chore: add test for yaml anchors
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
test-operation.yaml
|
test-operation.yaml
|
||||||
.idea/
|
.idea/
|
||||||
|
*.iml
|
||||||
@@ -669,6 +669,52 @@ externalDocs:
|
|||||||
assert.Len(t, err, 1)
|
assert.Len(t, err, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreateDocument_YamlAnchor(t *testing.T) {
|
||||||
|
// load petstore into bytes
|
||||||
|
anchorDocument, _ := os.ReadFile("../../../test_specs/yaml-anchor.yaml")
|
||||||
|
|
||||||
|
// read in specification
|
||||||
|
info, _ := datamodel.ExtractSpecInfo(anchorDocument)
|
||||||
|
|
||||||
|
// build low-level document model
|
||||||
|
document, errors := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
|
||||||
|
AllowFileReferences: false,
|
||||||
|
AllowRemoteReferences: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
// if something went wrong, a slice of errors is returned
|
||||||
|
if len(errors) > 0 {
|
||||||
|
for i := range errors {
|
||||||
|
fmt.Printf("error: %s\n", errors[i].Error())
|
||||||
|
}
|
||||||
|
panic("cannot build document")
|
||||||
|
}
|
||||||
|
|
||||||
|
examplePath := document.Paths.Value.FindPath("/system/examples/{id}")
|
||||||
|
assert.NotNil(t, examplePath)
|
||||||
|
|
||||||
|
// Check tag reference
|
||||||
|
getOp := examplePath.Value.Get.Value
|
||||||
|
assert.NotNil(t, getOp)
|
||||||
|
postOp := examplePath.Value.Get.Value
|
||||||
|
assert.NotNil(t, postOp)
|
||||||
|
assert.Equal(t, 1, len(getOp.GetTags().Value))
|
||||||
|
assert.Equal(t, 1, len(postOp.GetTags().Value))
|
||||||
|
assert.Equal(t, getOp.GetTags().Value, postOp.GetTags().Value)
|
||||||
|
|
||||||
|
// Check paramter reference
|
||||||
|
|
||||||
|
getParams := examplePath.Value.Get.Value.Parameters.Value
|
||||||
|
assert.NotNil(t, getParams)
|
||||||
|
postParams := examplePath.Value.Post.Value.Parameters.Value
|
||||||
|
assert.NotNil(t, postParams)
|
||||||
|
|
||||||
|
assert.Equal(t, 1, len(getParams))
|
||||||
|
assert.Equal(t, 1, len(postParams))
|
||||||
|
assert.Equal(t, getParams, postParams)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleCreateDocument() {
|
func ExampleCreateDocument() {
|
||||||
// How to create a low-level OpenAPI 3 Document
|
// How to create a low-level OpenAPI 3 Document
|
||||||
|
|
||||||
|
|||||||
85
test_specs/yaml-anchor.yaml
Normal file
85
test_specs/yaml-anchor.yaml
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
openapi: 3.0.2
|
||||||
|
info:
|
||||||
|
title: Example
|
||||||
|
version: 0.0.1
|
||||||
|
components:
|
||||||
|
securitySchemes:
|
||||||
|
bearerAuth:
|
||||||
|
type: http
|
||||||
|
scheme: bearer
|
||||||
|
bearerFormat: JWT
|
||||||
|
schemas:
|
||||||
|
Error:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
message:
|
||||||
|
type: string
|
||||||
|
description: Error message
|
||||||
|
Example:
|
||||||
|
type: object
|
||||||
|
required:
|
||||||
|
- id
|
||||||
|
properties:
|
||||||
|
id:
|
||||||
|
type: string
|
||||||
|
title: Name
|
||||||
|
pattern: ^[a-zA-Z0-9_\-]+$
|
||||||
|
description: Name of the Example.
|
||||||
|
description:
|
||||||
|
type: string
|
||||||
|
title: Description
|
||||||
|
description: Brief description of this Example. Optional.
|
||||||
|
security:
|
||||||
|
- bearerAuth: []
|
||||||
|
paths:
|
||||||
|
/system/examples/{id}:
|
||||||
|
get:
|
||||||
|
tags:
|
||||||
|
&a1
|
||||||
|
- Examples
|
||||||
|
summary: Get a list of Example objects
|
||||||
|
parameters:
|
||||||
|
&id
|
||||||
|
- name: id
|
||||||
|
in: path
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
description: Unique ID
|
||||||
|
description: Get a list of Example objects
|
||||||
|
responses:
|
||||||
|
&a2
|
||||||
|
"200":
|
||||||
|
description: a list of Example objects
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
count:
|
||||||
|
type: integer
|
||||||
|
description: number of items present in the items array
|
||||||
|
items:
|
||||||
|
type: array
|
||||||
|
items:
|
||||||
|
$ref: "#/components/schemas/Example"
|
||||||
|
"401":
|
||||||
|
description: Unauthorized
|
||||||
|
"500":
|
||||||
|
description: Unexpected error
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Error"
|
||||||
|
post:
|
||||||
|
tags: *a1
|
||||||
|
parameters: *id
|
||||||
|
summary: Create Example
|
||||||
|
description: Create Example
|
||||||
|
requestBody:
|
||||||
|
description: New Example object
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Example"
|
||||||
|
responses: *a2
|
||||||
Reference in New Issue
Block a user