Repository: ogen-go/ogen
Pull request #308 (2022-04-23 12:07:56)

fix(parser): check requestBody for nil


Patch:
diff --git a/openapi/parser/parse_request_body.go b/openapi/parser/parse_request_body.go
index f4c91dea..9eab324a 100644
--- a/openapi/parser/parse_request_body.go
+++ b/openapi/parser/parse_request_body.go
@@ -9,6 +9,9 @@ import (
 )
 
 func (p *parser) parseRequestBody(body *ogen.RequestBody, ctx resolveCtx) (*openapi.RequestBody, error) {
+	if body == nil {
+		return nil, errors.New("body is empty or nil")
+	}
 	if ref := body.Ref; ref != "" {
 		reqBody, err := p.resolveRequestBody(ref, ctx)
 		if err != nil {


Test patch:
diff --git a/_testdata/negative/null_default_value.json b/_testdata/negative/null_default_value.json
index ede2e900..613799af 100644
--- a/_testdata/negative/null_default_value.json
+++ b/_testdata/negative/null_default_value.json
@@ -7,8 +7,6 @@
   "paths": {
     "/healthz": {
       "get": {
-        "operationId": "probeLiveness",
-        "description": "Liveness probe for kubernetes",
         "responses": {
           "200": {
             "description": "User info",
@@ -28,42 +26,6 @@
                 }
               }
             }
-          },
-          "default": {
-            "$ref": "#/components/responses/Error"
-          }
-        }
-      }
-    }
-  },
-  "components": {
-    "schemas": {
-      "Error": {
-        "description": "Represents error object",
-        "type": "object",
-        "properties": {
-          "code": {
-            "type": "integer",
-            "format": "int64"
-          },
-          "message": {
-            "type": "string"
-          }
-        },
-        "required": [
-          "code",
-          "message"
-        ]
-      }
-    },
-    "responses": {
-      "Error": {
-        "description": "General Error",
-        "content": {
-          "application/json": {
-            "schema": {
-              "$ref": "#/components/schemas/Error"
-            }
           }
         }
       }
diff --git a/_testdata/negative/null_request_body.json b/_testdata/negative/null_request_body.json
new file mode 100644
index 00000000..0bdc68b3
--- /dev/null
+++ b/_testdata/negative/null_request_body.json
@@ -0,0 +1,33 @@
+{
+  "openapi": "3.0.0",
+  "info": {
+    "title": "title",
+    "version": "v0.1.0"
+  },
+  "paths": {
+    "/foo": {
+      "get": {
+        "requestBody": {
+          "$ref": "#/components/requestBodies/Foo"
+        },
+        "responses": {
+          "default": {
+            "description": "default",
+            "content": {
+              "application/json": {
+                "schema": {
+                  "type": "object"
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+  },
+  "components": {
+    "requestBodies": {
+      "Foo": null
+    }
+  }
+}
diff --git a/_testdata/negative/null_request_parameter.json b/_testdata/negative/null_request_parameter.json
index 2702528f..1e0849ca 100644
--- a/_testdata/negative/null_request_parameter.json
+++ b/_testdata/negative/null_request_parameter.json
@@ -7,53 +7,10 @@
   "paths": {
     "/healthz": {
       "get": {
-        "operationId": "probeLiveness",
-        "description": "Liveness probe for kubernetes",
         "parameters": [
           null
-        ],
-        "responses": {
-          "200": {
-            "description": "User info",
-            "content": {
-              "application/json": {
-                "schema": {
-                  "type": "string"
-                }
-              }
-            }
-          },
-          "default": {
-            "$ref": "#/components/responses/Error"
-          }
-        }
-      }
-    }
-  },
-  "components": {
-    "schemas": {
-      "Error": {
-        "description": "Represents error object",
-        "type": "object",
-        "properties": {
-          "code": {
-            "type": "integer",
-            "format": "int64"
-          },
-          "message": {
-            "type": "string"
-          }
-        },
-        "required": [
-          "code",
-          "message"
         ]
       }
-    },
-    "responses": {
-      "Error": {
-        "$ref": "#/components/responses/Error"
-      }
     }
   }
 }
diff --git a/_testdata/negative/null_response.json b/_testdata/negative/null_response.json
index ac3c3a16..ac594f8f 100644
--- a/_testdata/negative/null_response.json
+++ b/_testdata/negative/null_response.json
@@ -7,8 +7,6 @@
   "paths": {
     "/healthz": {
       "get": {
-        "operationId": "probeLiveness",
-        "description": "Liveness probe for kubernetes",
         "responses": {
           "200": null
         }