File: issue341_test.go

package info (click to toggle)
golang-github-getkin-kin-openapi 0.124.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,288 kB
  • sloc: sh: 344; makefile: 4
file content (80 lines) | stat: -rw-r--r-- 1,417 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
package openapi3

import (
	"context"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestIssue341(t *testing.T) {
	sl := NewLoader()
	sl.IsExternalRefsAllowed = true
	doc, err := sl.LoadFromFile("testdata/main.yaml")
	require.NoError(t, err)

	err = doc.Validate(sl.Context)
	require.NoError(t, err)

	err = sl.ResolveRefsIn(doc, nil)
	require.NoError(t, err)

	bs, err := doc.MarshalJSON()
	require.NoError(t, err)
	require.JSONEq(t, `{
	"info": {
		"title": "test file",
		"version": "n/a"
	},
	"openapi": "3.0.0",
	"paths": {
		"/testpath": {
			"$ref": "testpath.yaml#/paths/~1testpath"
		}
	}
}`, string(bs))

	require.Equal(t, &Types{"string"}, doc.
		Paths.Value("/testpath").
		Get.
		Responses.Value("200").Value.
		Content["application/json"].
		Schema.Value.
		Type)

	doc.InternalizeRefs(context.Background(), nil)
	bs, err = doc.MarshalJSON()
	require.NoError(t, err)
	require.JSONEq(t, `{
		"components": {
		  "responses": {
			"testpath_200_response": {
			  "content": {
				"application/json": {
				  "schema": {
					"type": "string"
				  }
				}
			  },
			  "description": "a custom response"
			}
		  }
		},
		"info": {
		  "title": "test file",
		  "version": "n/a"
		},
		"openapi": "3.0.0",
		"paths": {
		  "/testpath": {
			"get": {
			  "responses": {
				"200": {
				  "$ref": "#/components/responses/testpath_200_response"
				}
			  }
			}
		  }
		}
	  }`, string(bs))
}