@@ -364,13 +364,13 @@ func TestAddOperation(t *testing.T) {
364364 Summary : "ABC" ,
365365 Description : "XYZ" ,
366366 Deprecated : true ,
367- Responses : []* OperationReponse {
368- & OperationReponse {
367+ Responses : []* OperationResponse {
368+ & OperationResponse {
369369 Code : "400" ,
370370 Description : "Bad Request" ,
371371 Model : CustomError {},
372372 },
373- & OperationReponse {
373+ & OperationResponse {
374374 Code : "5XX" ,
375375 Description : "Server Errors" ,
376376 },
@@ -516,21 +516,75 @@ func TestSetOperationResponseError(t *testing.T) {
516516 op := & Operation {
517517 Responses : make (Responses ),
518518 }
519- err := g .setOperationResponse (op , reflect .TypeOf (new (string )), "200" , "application/json" , "" , nil )
519+ err := g .setOperationResponse (op , reflect .TypeOf (new (string )), "200" , "application/json" , "" , nil , nil , nil )
520520 assert .Nil (t , err )
521521
522522 // Add another response with same code.
523- err = g .setOperationResponse (op , reflect .TypeOf (new (int )), "200" , "application/xml" , "" , nil )
523+ err = g .setOperationResponse (op , reflect .TypeOf (new (int )), "200" , "application/xml" , "" , nil , nil , nil )
524524 assert .NotNil (t , err )
525525
526526 // Add invalid response code that cannot
527527 // be converted to an integer.
528- err = g .setOperationResponse (op , reflect .TypeOf (new (bool )), "two-hundred" , "" , "" , nil )
528+ err = g .setOperationResponse (op , reflect .TypeOf (new (bool )), "two-hundred" , "" , "" , nil , nil , nil )
529529 assert .NotNil (t , err )
530530
531531 // Add out of range response code.
532- err = g .setOperationResponse (op , reflect .TypeOf (new (bool )), "777" , "" , "" , nil )
532+ err = g .setOperationResponse (op , reflect .TypeOf (new (bool )), "777" , "" , "" , nil , nil , nil )
533533 assert .NotNil (t , err )
534+
535+ // Cannot set both example and examples
536+ err = g .setOperationResponse (op , reflect .TypeOf (new (bool )), "404" , "" , "" , nil , "notFoundExample" , map [string ]interface {}{"badRequest" : "message" })
537+ assert .NotNil (t , err )
538+ }
539+
540+ // TestSetOperationResponseExample tests that
541+ // one example is set correctly.
542+ func TestSetOperationResponseExample (t * testing.T ) {
543+ g := gen (t )
544+ op := & Operation {
545+ Responses : make (Responses ),
546+ }
547+
548+ error1 := map [string ]interface {}{"error" : "message1" }
549+
550+ err := g .setOperationResponse (op , reflect .TypeOf (new (string )), "400" , "application/json" , "" , nil , error1 , nil )
551+ assert .Nil (t , err )
552+
553+ // assert example set correctly
554+ mt := op .Responses ["400" ].Response .Content ["application/json" ].MediaType
555+ assert .Equal (t , error1 , mt .Example )
556+
557+ // examples should be empty
558+ assert .Nil (t , mt .Examples )
559+ }
560+
561+ // TestSetOperationResponseExamples tests that
562+ // multiple examples are set correctly.
563+ func TestSetOperationResponseExamples (t * testing.T ) {
564+ g := gen (t )
565+ op := & Operation {
566+ Responses : make (Responses ),
567+ }
568+
569+ error1 := map [string ]interface {}{"error" : "message1" }
570+ error2 := map [string ]interface {}{"error" : "message2" }
571+
572+ err := g .setOperationResponse (op , reflect .TypeOf (new (string )), "400" , "application/json" , "" , nil , nil ,
573+ map [string ]interface {}{
574+ "one" : error1 ,
575+ "two" : error2 ,
576+ },
577+ )
578+ assert .Nil (t , err )
579+
580+ // assert examples set correctly
581+ mt := op .Responses ["400" ].Response .Content ["application/json" ].MediaType
582+ assert .Equal (t , 2 , len (mt .Examples ))
583+ assert .Equal (t , error1 , mt .Examples ["one" ].Example .Value )
584+ assert .Equal (t , error2 , mt .Examples ["two" ].Example .Value )
585+
586+ // example should be empty
587+ assert .Nil (t , mt .Example )
534588}
535589
536590// TestSetOperationParamsError tests the various error
0 commit comments