Skip to content
Open
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
63 changes: 63 additions & 0 deletions examples/v3.1/yaml/xquik.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
openapi: 3.1.0
info:
title: Xquik API
version: "1.0"
servers:
- url: https://xquik.com
security:
- apiKey: []
paths:
/api/v1/x/tweets/search:
get:
operationId: searchTweets
summary: Search X posts
parameters:
- name: q
in: query
required: true
schema:
type: string
- name: limit
in: query
schema:
type: integer
minimum: 1
maximum: 100
responses:
"200":
description: Search results
content:
application/json:
schema:
type: object
required:
- data
properties:
data:
type: array
items:
$ref: "#/components/schemas/Tweet"
components:
securitySchemes:
apiKey:
type: apiKey
in: header
name: x-api-key
schemas:
Tweet:
type: object
required:
- id
- text
- authorHandle
- createdAt
properties:
id:
type: string
text:
type: string
authorHandle:
type: string
createdAt:
type: string
format: date-time
4 changes: 3 additions & 1 deletion jsonwriter/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ func (w *writer) writeMap(node *yaml.Node, indent string) {
for i := 0; i < len(node.Content); i += 2 {
// first print the key
key := node.Content[i].Value
w.writeString(fmt.Sprintf("%s\"%+v\": ", innerIndent, key))
w.writeString(innerIndent)
w.writeString(strconv.Quote(key))
w.writeString(": ")
// then the value
value := node.Content[i+1]
switch value.Kind {
Expand Down
12 changes: 12 additions & 0 deletions jsonwriter/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestMarshal(t *testing.T) {
sequenceSequenceStringArrayTestCase(),
sequenceMappingNodeTestCase(),
mappingNodeTestCase(),
mappingNodeEscapedKeyTestCase(),
documentNodeTestCase(),
aliasNodeTestCase(),
}
Expand Down Expand Up @@ -193,6 +194,17 @@ func mappingNodeTestCase() *MarshalTestCase {
}
}

func mappingNodeEscapedKeyTestCase() *MarshalTestCase {
node := compiler.NewMappingNode()
node.Content = append(node.Content, compiler.NewScalarNodeForString("a\\b\nc\""))
node.Content = append(node.Content, compiler.NewScalarNodeForString("value"))
return &MarshalTestCase{
Name: "Mapping node with escaped key",
Node: node,
Expected: "{\n \"a\\\\b\\nc\\\"\": \"value\"\n}\n",
}
}

func documentNodeTestCase() *MarshalTestCase {
m := compiler.NewMappingNode()
m.Content = append(m.Content, compiler.NewScalarNodeForString("version"))
Expand Down
33 changes: 31 additions & 2 deletions openapiv3/openapiv3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
package openapi_v3

import (
"io/ioutil"
"os"
"testing"
)

func TestParseDocument(t *testing.T) {
filename := "../examples/v3.0/yaml/petstore.yaml"
b, err := ioutil.ReadFile(filename)
b, err := os.ReadFile(filename)
if err != nil {
t.Logf("unable to read file %s", filename)
t.FailNow()
Expand All @@ -37,6 +37,35 @@ func TestParseDocument(t *testing.T) {
}
}

func TestParseDocument_XquikOpenAPI31(t *testing.T) {
filename := "../examples/v3.1/yaml/xquik.yaml"
b, err := os.ReadFile(filename)
if err != nil {
t.Logf("unable to read file %s", filename)
t.FailNow()
}
d, err := ParseDocument(b)
if err != nil {
t.Logf("%s", err.Error())
t.FailNow()
}
if d.Info.Title != "Xquik API" {
t.Errorf("unexpected value for Title: %s", d.Info.Title)
}
if !documentHasPath(d, "/api/v1/x/tweets/search") {
t.Errorf("missing Xquik search path")
}
}

func documentHasPath(document *Document, name string) bool {
for _, path := range document.Paths.Path {
if path.Name == name {
return true
}
}
return false
}

func TestParseDocument_Empty(t *testing.T) {
for _, test := range []struct {
name string
Expand Down