@@ -993,6 +993,60 @@ func generateErrorsFile(s spec.Swagger) (string, error) {
993993 }
994994 }
995995
996+ subrouters , err := swagger .ParseSubrouters (s )
997+ if err != nil {
998+ return "" , err
999+ }
1000+
1001+ for _ , router := range subrouters {
1002+ subspec , err := swagger .LoadSubrouterSpec (router )
1003+ if err != nil {
1004+ return "" , err
1005+ }
1006+
1007+ for _ , pathKey := range swagger .SortedPathItemKeys (subspec .Paths .Paths ) {
1008+ path := subspec .Paths .Paths [pathKey ]
1009+ pathItemOps := swagger .PathItemOperations (path )
1010+ for _ , opKey := range swagger .SortedOperationsKeys (pathItemOps ) {
1011+ op := pathItemOps [opKey ]
1012+ for _ , statusCode := range swagger .SortedStatusCodeKeys (op .Responses .StatusCodeResponses ) {
1013+ if statusCode < 400 {
1014+ continue
1015+ }
1016+ typeName , _ := swagger .OutputType (& s , op , statusCode )
1017+ if strings .HasPrefix (typeName , "models." ) {
1018+ typeName = typeName [7 :]
1019+ }
1020+ if typeNames .Contains (typeName ) {
1021+ log .Printf (
1022+ "Duplicate type name %s declared in %s subrouter spec\n " ,
1023+ typeName ,
1024+ router .Key ,
1025+ )
1026+ continue
1027+ }
1028+ typeNames .Add (typeName )
1029+
1030+ etype := errorType {
1031+ StatusCode : statusCode ,
1032+ Name : typeName ,
1033+ }
1034+
1035+ if schema , ok := subspec .Definitions [typeName ]; ! ok {
1036+ log .Printf ("TODO: could not find schema for %s, JS documentation will be incomplete" , typeName )
1037+ } else if len (schema .Properties ) > 0 {
1038+ for _ , name := range swagger .SortedSchemaProperties (schema ) {
1039+ propertySchema := schema .Properties [name ]
1040+ etype .JSDocProperties = append (etype .JSDocProperties , jsDocPropertyFromSchema (name , & propertySchema ))
1041+ }
1042+ }
1043+
1044+ typesTmpl .ErrorTypes = append (typesTmpl .ErrorTypes , etype )
1045+ }
1046+ }
1047+ }
1048+ }
1049+
9961050 return templates .WriteTemplate (typeTmplString , typesTmpl )
9971051}
9981052
@@ -1165,6 +1219,51 @@ func getErrorTypes(s spec.Swagger) ([]string, error) {
11651219 }
11661220 }
11671221 }
1222+
1223+ subrouters , err := swagger .ParseSubrouters (s )
1224+ if err != nil {
1225+ return nil , err
1226+ }
1227+
1228+ for _ , router := range subrouters {
1229+ routerSpec , err := swagger .LoadSubrouterSpec (router )
1230+ if err != nil {
1231+ return nil , err
1232+ }
1233+
1234+ for _ , pathKey := range swagger .SortedPathItemKeys (routerSpec .Paths .Paths ) {
1235+ path := routerSpec .Paths .Paths [pathKey ]
1236+ pathItemOps := swagger .PathItemOperations (path )
1237+ for _ , opKey := range swagger .SortedOperationsKeys (pathItemOps ) {
1238+ op := pathItemOps [opKey ]
1239+ for _ , statusCode := range swagger .SortedStatusCodeKeys (op .Responses .StatusCodeResponses ) {
1240+ if statusCode < 400 {
1241+ continue
1242+ }
1243+ typeName , _ := swagger .OutputType (routerSpec , op , statusCode )
1244+ if strings .HasPrefix (typeName , "models." ) {
1245+ typeName = typeName [7 :]
1246+ }
1247+
1248+ if _ , exists := typeNames [typeName ]; exists {
1249+ continue
1250+ }
1251+ typeNames [typeName ] = struct {}{}
1252+
1253+ if schema , ok := routerSpec .Definitions [typeName ]; ! ok {
1254+ errorTypes = append (errorTypes , fmt .Sprintf ("class %s {}" , typeName ))
1255+ } else if len (schema .Properties ) > 0 {
1256+ declaration , err := generateErrorDeclaration (& schema , typeName , "models." )
1257+ if err != nil {
1258+ return errorTypes , err
1259+ }
1260+ errorTypes = append (errorTypes , declaration )
1261+ }
1262+ }
1263+ }
1264+ }
1265+ }
1266+
11681267 return errorTypes , nil
11691268}
11701269
0 commit comments