File: openapi3gen_test.go

package info (click to toggle)
golang-github-getkin-kin-openapi 0.1.0%2Bgit20181119.fa639d0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 480 kB
  • sloc: makefile: 2
file content (112 lines) | stat: -rw-r--r-- 2,396 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package openapi3gen_test

import (
	"encoding/json"
	"testing"
	"time"

	"github.com/getkin/kin-openapi/openapi3gen"
	"github.com/stretchr/testify/require"
)

type CyclicType0 struct {
	CyclicField *CyclicType1 `json:"a"`
}
type CyclicType1 struct {
	CyclicField *CyclicType0 `json:"b"`
}

func TestCyclic(t *testing.T) {
	schema, refsMap, err := openapi3gen.NewSchemaRefForValue(&CyclicType0{})
	require.IsType(t, &openapi3gen.CycleError{}, err)
	require.Nil(t, schema)
	require.Empty(t, refsMap)
}

func TestSimple(t *testing.T) {
	type ExampleChild string
	type Example struct {
		Bool    bool                     `json:"bool"`
		Int     int                      `json:"int"`
		Int64   int64                    `json:"int64"`
		Float64 float64                  `json:"float64"`
		String  string                   `json:"string"`
		Bytes   []byte                   `json:"bytes"`
		JSON    json.RawMessage          `json:"json"`
		Time    time.Time                `json:"time"`
		Slice   []*ExampleChild          `json:"slice"`
		Map     map[string]*ExampleChild `json:"map"`
		Struct  struct {
			X string `json:"x"`
		} `json:"struct"`
		EmptyStruct struct {
			X string
		} `json:"structWithoutFields"`
		Ptr *ExampleChild `json:"ptr"`
	}

	schema, refsMap, err := openapi3gen.NewSchemaRefForValue(&Example{})
	require.NoError(t, err)
	require.Len(t, refsMap, 14)
	data, err := json.Marshal(schema)
	require.NoError(t, err)
	require.JSONEq(t, expectedSimple, string(data))
}

const expectedSimple = `
{
  "type": "object",
  "properties": {
    "bool": {
      "type": "boolean"
    },
    "int": {
      "type": "integer",
      "format": "int64"
    },
    "int64": {
      "type": "integer",
      "format": "int64"
    },
    "float64": {
      "type": "number"
    },
    "time": {
      "type": "string",
      "format": "date-time"
    },
    "string": {
      "type": "string"
    },
    "bytes": {
      "type": "string",
      "format": "byte"
    },
    "json": {},
    "slice": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "map": {
      "type": "object",
      "additionalProperties": {
        "type": "string"
      }
    },
    "struct": {
      "type": "object",
      "properties": {
        "x": {
          "type": "string"
        }
      }
    },
    "structWithoutFields": {},
    "ptr": {
      "type": "string"
    }
  }
}
`