Skip to content

Commit

Permalink
chore: add test for yaml anchors
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasRooney authored and daveshanley committed Jul 18, 2023
1 parent 13c3ae9 commit c9cf000
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
test-operation.yaml
.idea/
*.iml
46 changes: 46 additions & 0 deletions datamodel/low/v3/create_document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,52 @@ externalDocs:
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() {
// How to create a low-level OpenAPI 3 Document

Expand Down
85 changes: 85 additions & 0 deletions test_specs/yaml-anchor.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c9cf000

Please sign in to comment.