k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/validation/spec/responses_test.go (about)

     1  /*
     2   Copyright 2022 The Kubernetes Authors.
     3  
     4   Licensed under the Apache License, Version 2.0 (the "License");
     5   you may not use this file except in compliance with the License.
     6   You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10   Unless required by applicable law or agreed to in writing, software
    11   distributed under the License is distributed on an "AS IS" BASIS,
    12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   See the License for the specific language governing permissions and
    14   limitations under the License.
    15  */
    16  
    17  package spec
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  	jsontesting "k8s.io/kube-openapi/pkg/util/jsontesting"
    24  )
    25  
    26  func TestResponsesRoundtrip(t *testing.T) {
    27  	cases := []jsontesting.RoundTripTestCase{
    28  		{
    29  			// Show at least one field from each embededd struct sitll allows
    30  			// roundtrips successfully
    31  			Name: "UnmarshalEmbedded",
    32  			JSON: `{
    33  				"default": {
    34  					"$ref": "/components/some/ref.foo"
    35  				},
    36  				"x-framework": "swagger-go"
    37  			  }`,
    38  			Object: &Responses{
    39  				VendorExtensible: VendorExtensible{
    40  					Extensions: Extensions{
    41  						"x-framework": "swagger-go",
    42  					},
    43  				},
    44  				ResponsesProps: ResponsesProps{
    45  					Default: &Response{
    46  						Refable: Refable{Ref: MustCreateRef("/components/some/ref.foo")},
    47  					},
    48  				},
    49  			},
    50  		},
    51  		{
    52  			Name: "Decode Ref Object",
    53  			Object: &Responses{
    54  				VendorExtensible: VendorExtensible{Extensions: map[string]interface{}{
    55  					"x-<vBŤç,ʡËdSS暙ɑɮ":     "鄲兴ȑʦ衈覻鋕嚮峡jw逓:鮕虫F迢.",
    56  					"x-h":                  "",
    57  					"x-岡ʍ":                 "Đɻ/nnjo鿻曑Œ TĀyĢ",
    58  					"x-绅ƄȆ疩ã[魑銒;苎#砠zPȺ5Aù": "閲ljǠyư"},
    59  				},
    60  				ResponsesProps: ResponsesProps{
    61  					Default: &Response{
    62  						ResponseProps: ResponseProps{Description: "梱bȿF)渽Ɲō-%x"},
    63  					},
    64  					StatusCodeResponses: map[int]Response{
    65  						200: {
    66  							Refable: Refable{Ref: MustCreateRef("Cat")},
    67  						},
    68  					},
    69  				},
    70  			},
    71  		},
    72  		{
    73  			Name: "Default Full Object",
    74  			Object: &Responses{
    75  				VendorExtensible: VendorExtensible{Extensions: map[string]interface{}{
    76  					"x-<vBŤç,ʡËdSS暙ɑɮ":     "鄲兴ȑʦ衈覻鋕嚮峡jw逓:鮕虫F迢.",
    77  					"x-h":                  "",
    78  					"x-岡ʍ":                 "Đɻ/nnjo鿻曑Œ TĀyĢ",
    79  					"x-绅ƄȆ疩ã[魑銒;苎#砠zPȺ5Aù": "閲ljǠyư"},
    80  				},
    81  				ResponsesProps: ResponsesProps{
    82  					Default: &Response{
    83  						Refable: Refable{Ref: MustCreateRef("Dog")},
    84  					},
    85  					StatusCodeResponses: map[int]Response{
    86  						200: {
    87  							ResponseProps: ResponseProps{
    88  								Description: "梱bȿF)渽Ɲō-%x",
    89  								Headers: map[string]Header{
    90  									"a header": header,
    91  								},
    92  								Schema: &Schema{
    93  									VendorExtensible: VendorExtensible{Extensions: map[string]interface{}{"x-framework": "go-swagger"}},
    94  									SchemaProps: SchemaProps{
    95  										Ref:              MustCreateRef("Cat"),
    96  										Type:             []string{"string"},
    97  										Format:           "date",
    98  										Description:      "the description of this schema",
    99  										Title:            "the title",
   100  										Default:          "blah",
   101  										Maximum:          float64Ptr(100),
   102  										ExclusiveMaximum: true,
   103  										ExclusiveMinimum: true,
   104  										Minimum:          float64Ptr(5),
   105  										MaxLength:        int64Ptr(100),
   106  										MinLength:        int64Ptr(5),
   107  										Pattern:          "\\w{1,5}\\w+",
   108  										MaxItems:         int64Ptr(100),
   109  										MinItems:         int64Ptr(5),
   110  										UniqueItems:      true,
   111  										MultipleOf:       float64Ptr(5),
   112  										Enum:             []interface{}{"hello", "world"},
   113  										MaxProperties:    int64Ptr(5),
   114  										MinProperties:    int64Ptr(1),
   115  										Required:         []string{"id", "name"},
   116  										Items:            &SchemaOrArray{Schema: &Schema{SchemaProps: SchemaProps{Type: []string{"string"}}}},
   117  										AllOf:            []Schema{{SchemaProps: SchemaProps{Type: []string{"string"}}}},
   118  										Properties: map[string]Schema{
   119  											"id":   {SchemaProps: SchemaProps{Type: []string{"integer"}, Format: "int64"}},
   120  											"name": {SchemaProps: SchemaProps{Type: []string{"string"}}},
   121  										},
   122  										AdditionalProperties: &SchemaOrBool{Allows: true, Schema: &Schema{SchemaProps: SchemaProps{
   123  											Type:   []string{"integer"},
   124  											Format: "int32",
   125  										}}},
   126  									},
   127  									SwaggerSchemaProps: SwaggerSchemaProps{
   128  										Discriminator: "not this",
   129  										ReadOnly:      true,
   130  										ExternalDocs: &ExternalDocumentation{
   131  											Description: "the documentation etc",
   132  											URL:         "http://readthedocs.org/swagger",
   133  										},
   134  										Example: []interface{}{
   135  											map[string]interface{}{
   136  												"id":   float64(1),
   137  												"name": "a book",
   138  											},
   139  											map[string]interface{}{
   140  												"id":   float64(2),
   141  												"name": "the thing",
   142  											},
   143  										},
   144  									},
   145  								},
   146  								Examples: map[string]interface{}{
   147  									"example1": "example text",
   148  								},
   149  							},
   150  						},
   151  					},
   152  				},
   153  			},
   154  		},
   155  		{
   156  			// Show we cannot decode a string into something expecting object
   157  			Name:                   "FailDecodeDefault",
   158  			JSON:                   `{"x-extension":"an extension","default":"wrong type object"}`,
   159  			ExpectedUnmarshalError: "unmarshal JSON string into Go value of type struct",
   160  		},
   161  	}
   162  
   163  	for _, tcase := range cases {
   164  		t.Run(tcase.Name, func(t *testing.T) {
   165  			require.NoError(t, tcase.RoundTripTest(&Responses{}))
   166  		})
   167  	}
   168  }