Repository: ogen-go/ogen
Pull request #186 (2022-02-14 16:42:11)

fix(gen): unnamed response validation


Patch:
diff --git a/gen/_template/validators.tmpl b/gen/_template/validators.tmpl
index 14756c00..a87a0150 100644
--- a/gen/_template/validators.tmpl
+++ b/gen/_template/validators.tmpl
@@ -134,7 +134,7 @@ func (s {{ $t.Name }}) Validate() error {
 			{{- template "validate" field_elem $f }}
 		}(); err != nil {
 			failures = append(failures, validate.FieldError{
-				Name: {{ quote $f.Spec.Name }},
+				Name: {{ quote $f.ValidationName }},
 				Error: err,
 			})
 		}
@@ -154,7 +154,7 @@ func (s {{ $t.Name }}) Validate() error {
 			{{- template "validate" field_elem $f }}
 		}(); err != nil {
 			failures = append(failures, validate.FieldError{
-				Name: {{ quote $f.Spec.Name }},
+				Name: {{ quote $f.ValidationName }},
 				Error: err,
 			})
 		}
diff --git a/internal/ir/field.go b/internal/ir/field.go
index 347b647d..37c68067 100644
--- a/internal/ir/field.go
+++ b/internal/ir/field.go
@@ -25,6 +25,14 @@ type Field struct {
 	Spec *jsonschema.Property
 }
 
+// ValidationName returns name for FieldError.
+func (f Field) ValidationName() string {
+	if f.Spec != nil {
+		return f.Spec.Name
+	}
+	return f.Name
+}
+
 // Default returns default value of this field, if it is set.
 func (f Field) Default() Default {
 	var schema *jsonschema.Schema


Test patch:
diff --git a/_testdata/positive/unnamed-response-validation.json b/_testdata/positive/unnamed-response-validation.json
new file mode 100644
index 00000000..755a8c64
--- /dev/null
+++ b/_testdata/positive/unnamed-response-validation.json
@@ -0,0 +1,33 @@
+{
+    "openapi": "3.0.0",
+    "info": {
+        "title": "Objects",
+        "version": "0.0.0"
+    },
+    "paths": {
+        "/objects": {
+            "get": {
+                "responses": {
+                    "default": {
+                        "description": "Success",
+                        "content": {
+                            "application/json": {
+                                "schema": {
+                                    "type": "object",
+                                    "properties": {
+                                        "alpha": {
+                                            "type": "string",
+                                            "writeOnly": true
+                                        }
+                                    },
+                                    "minProperties": 1,
+                                    "additionalProperties": false
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+}
diff --git a/gen_test.go b/gen_test.go
index 77430935..250118da 100644
--- a/gen_test.go
+++ b/gen_test.go
@@ -109,7 +109,11 @@ func TestGenerate(t *testing.T) {
 		if !ok {
 			skip = []string{"all"}
 		}
-		t.Run(strings.TrimSuffix(file, ".json"), g(path, skip...))
+
+		file = strings.TrimSuffix(file, ".json")
+		file = strings.TrimSuffix(file, ".yml")
+		file = strings.TrimSuffix(file, ".yaml")
+		t.Run(file, g(path, skip...))
 		return nil
 	}); err != nil {
 		t.Fatal(err)