@@ -114,13 +114,17 @@ func init() {
114114 // api.Status is returned in errors
115115
116116 // "internal" version
117- api .Scheme .AddKnownTypes ("" , & Simple {}, & SimpleList {}, & api.Status {}, & api.ListOptions {})
117+ api .Scheme .AddKnownTypes ("" , & Simple {}, & SimpleList {}, & api.Status {}, & api.ListOptions {}, & SimpleGetOptions {} )
118118 // "version" version
119119 // TODO: Use versioned api objects?
120- api .Scheme .AddKnownTypes (testVersion , & Simple {}, & SimpleList {}, & v1beta1.Status {})
120+ api .Scheme .AddKnownTypes (testVersion , & Simple {}, & SimpleList {}, & v1beta1.Status {}, & SimpleGetOptions {} )
121121 // "version2" version
122122 // TODO: Use versioned api objects?
123- api .Scheme .AddKnownTypes (testVersion2 , & Simple {}, & SimpleList {}, & v1beta3.Status {})
123+ api .Scheme .AddKnownTypes (testVersion2 , & Simple {}, & SimpleList {}, & v1beta3.Status {}, & SimpleGetOptions {})
124+
125+ // Register SimpleGetOptions with the server versions to convert query params to it
126+ api .Scheme .AddKnownTypes ("v1beta1" , & SimpleGetOptions {})
127+ api .Scheme .AddKnownTypes ("v1beta3" , & SimpleGetOptions {})
124128
125129 nsMapper := newMapper ()
126130 legacyNsMapper := newMapper ()
@@ -231,6 +235,14 @@ type Simple struct {
231235
232236func (* Simple ) IsAnAPIObject () {}
233237
238+ type SimpleGetOptions struct {
239+ api.TypeMeta `json:",inline"`
240+ Param1 string `json:"param1"`
241+ Param2 string `json:"param2"`
242+ }
243+
244+ func (* SimpleGetOptions ) IsAnAPIObject () {}
245+
234246type SimpleList struct {
235247 api.TypeMeta `json:",inline"`
236248 api.ListMeta `json:"metadata,inline"`
@@ -254,6 +266,21 @@ func TestSimpleSetupRight(t *testing.T) {
254266 }
255267}
256268
269+ func TestSimpleOptionsSetupRight (t * testing.T ) {
270+ s := & SimpleGetOptions {}
271+ wire , err := codec .Encode (s )
272+ if err != nil {
273+ t .Fatal (err )
274+ }
275+ s2 , err := codec .Decode (wire )
276+ if err != nil {
277+ t .Fatal (err )
278+ }
279+ if ! reflect .DeepEqual (s , s2 ) {
280+ t .Fatalf ("encode/decode broken:\n %#v\n %#v\n " , s , s2 )
281+ }
282+ }
283+
257284type SimpleRESTStorage struct {
258285 errors map [string ]error
259286 list []Simple
@@ -314,10 +341,10 @@ func (s *SimpleStream) Close() error {
314341
315342func (s * SimpleStream ) IsAnAPIObject () {}
316343
317- func (s * SimpleStream ) InputStream (version , accept string ) (io.ReadCloser , string , error ) {
344+ func (s * SimpleStream ) InputStream (version , accept string ) (io.ReadCloser , bool , string , error ) {
318345 s .version = version
319346 s .accept = accept
320- return s , s .contentType , s .err
347+ return s , false , s .contentType , s .err
321348}
322349
323350func (storage * SimpleRESTStorage ) Get (ctx api.Context , id string ) (runtime.Object , error ) {
@@ -432,6 +459,23 @@ func (m *MetadataRESTStorage) ProducesMIMETypes(method string) []string {
432459 return m .types
433460}
434461
462+ type GetWithOptionsRESTStorage struct {
463+ * SimpleRESTStorage
464+ optionsReceived runtime.Object
465+ }
466+
467+ func (r * GetWithOptionsRESTStorage ) Get (ctx api.Context , name string , options runtime.Object ) (runtime.Object , error ) {
468+ if _ , ok := options .(* SimpleGetOptions ); ! ok {
469+ return nil , fmt .Errorf ("Unexpected options object: %#v" , options )
470+ }
471+ r .optionsReceived = options
472+ return r .SimpleRESTStorage .Get (ctx , name )
473+ }
474+
475+ func (r * GetWithOptionsRESTStorage ) NewGetOptions () runtime.Object {
476+ return & SimpleGetOptions {}
477+ }
478+
435479func extractBody (response * http.Response , object runtime.Object ) (string , error ) {
436480 defer response .Body .Close ()
437481 body , err := ioutil .ReadAll (response .Body )
@@ -878,6 +922,47 @@ func TestGetBinary(t *testing.T) {
878922 }
879923}
880924
925+ func TestGetWithOptions (t * testing.T ) {
926+ storage := map [string ]rest.Storage {}
927+ simpleStorage := GetWithOptionsRESTStorage {
928+ SimpleRESTStorage : & SimpleRESTStorage {
929+ item : Simple {
930+ Other : "foo" ,
931+ },
932+ },
933+ }
934+ storage ["simple" ] = & simpleStorage
935+ handler := handle (storage )
936+ server := httptest .NewServer (handler )
937+ defer server .Close ()
938+
939+ resp , err := http .Get (server .URL + "/api/version/simple/id?param1=test1¶m2=test2" )
940+ if err != nil {
941+ t .Fatalf ("unexpected error: %v" , err )
942+ }
943+ if resp .StatusCode != http .StatusOK {
944+ t .Fatalf ("unexpected response: %#v" , resp )
945+ }
946+ var itemOut Simple
947+ body , err := extractBody (resp , & itemOut )
948+ if err != nil {
949+ t .Errorf ("unexpected error: %v" , err )
950+ }
951+
952+ if itemOut .Name != simpleStorage .item .Name {
953+ t .Errorf ("Unexpected data: %#v, expected %#v (%s)" , itemOut , simpleStorage .item , string (body ))
954+ }
955+
956+ opts , ok := simpleStorage .optionsReceived .(* SimpleGetOptions )
957+ if ! ok {
958+ t .Errorf ("Unexpected options object received: %#v" , simpleStorage .optionsReceived )
959+ return
960+ }
961+ if opts .Param1 != "test1" || opts .Param2 != "test2" {
962+ t .Errorf ("Did not receive expected options: %#v" , opts )
963+ }
964+ }
965+
881966func TestGetAlternateSelfLink (t * testing.T ) {
882967 storage := map [string ]rest.Storage {}
883968 simpleStorage := SimpleRESTStorage {
0 commit comments