MCPcopy Index your code
hub / github.com/oapi-codegen/oapi-codegen / TestExampleOpenAPICodeGeneration

Function TestExampleOpenAPICodeGeneration

pkg/codegen/codegen_test.go:21–93  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

19)
20
21func TestExampleOpenAPICodeGeneration(t *testing.T) {
22
23 // Input vars for code generation:
24 packageName := "testswagger"
25 opts := Configuration{
26 PackageName: packageName,
27 Generate: GenerateOptions{
28 EchoServer: true,
29 Client: true,
30 Models: true,
31 EmbeddedSpec: true,
32 },
33 }
34
35 loader := openapi3.NewLoader()
36 loader.IsExternalRefsAllowed = true
37
38 // Get a spec from the test definition in this file:
39 swagger, err := loader.LoadFromData([]byte(testOpenAPIDefinition))
40 assert.NoError(t, err)
41
42 // Run our code generation:
43 code, err := Generate(swagger, opts)
44 assert.NoError(t, err)
45 assert.NotEmpty(t, code)
46
47 // Check that we have valid (formattable) code:
48 _, err = format.Source([]byte(code))
49 assert.NoError(t, err)
50
51 // Check that we have a package:
52 assert.Contains(t, code, "package testswagger")
53
54 // Check that response structs are generated correctly:
55 assert.Contains(t, code, "type GetTestByNameResponse struct {")
56
57 // Check that response structs contains fallbacks to interface for invalid types:
58 // Here an invalid array with no items.
59 assert.Contains(t, code, `
60type GetTestByNameResponse struct {
61 Body []byte
62 HTTPResponse *http.Response
63 JSON200 *[]Test
64 XML200 *[]Test
65 JSON422 *[]interface{}
66 XML422 *[]interface{}
67 JSONDefault *Error
68}`)
69
70 // Check that the helper methods are generated correctly:
71 assert.Contains(t, code, "func (r GetTestByNameResponse) Status() string {")
72 assert.Contains(t, code, "func (r GetTestByNameResponse) StatusCode() int {")
73 assert.Contains(t, code, "func ParseGetTestByNameResponse(rsp *http.Response) (*GetTestByNameResponse, error) {")
74
75 // Check the client method signatures:
76 assert.Contains(t, code, "type GetTestByNameParams struct {")
77 assert.Contains(t, code, "Top *int `form:\"$top,omitempty\" json:\"$top,omitempty\"`")
78 assert.Contains(t, code, "func (c *Client) GetTestByName(ctx context.Context, name string, params *GetTestByNameParams, reqEditors ...RequestEditorFn) (*http.Response, error) {")

Callers

nothing calls this directly

Calls 1

GenerateFunction · 0.85

Tested by

no test coverage detected