nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: getkin/kin-openapi
Pull request #610 (2022-09-21 13:45:20)

Validate default values against schema
Closes https://github.com/getkin/kin-openapi/issues/136


Patch:
diff --git a/openapi3/schema.go b/openapi3/schema.go
index ded97f0..9b36e60 100644
--- a/openapi3/schema.go
+++ b/openapi3/schema.go
@@ -753,6 +753,12 @@ func (schema *Schema) validate(ctx context.Context, stack []*Schema) (err error)
 		}
 	}
 
+	if v := schema.Default; v != nil {
+		if err := schema.VisitJSON(v); err != nil {
+			return fmt.Errorf("invalid schema default: %w", err)
+		}
+	}
+
 	if x := schema.Example; x != nil && !validationOpts.ExamplesValidationDisabled {
 		if err := validateExampleValue(x, schema); err != nil {
 			return fmt.Errorf("invalid schema example: %w", err)


Test patch:
diff --git a/openapi3/issue136_test.go b/openapi3/issue136_test.go
new file mode 100644
index 0000000..36c8b85
--- /dev/null
+++ b/openapi3/issue136_test.go
@@ -0,0 +1,53 @@
+package openapi3
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/require"
+)
+
+func TestIssue136(t *testing.T) {
+	specf := func(dflt string) string {
+		return `
+openapi: 3.0.2
+info:
+  title: "Hello World REST APIs"
+  version: "1.0"
+paths: {}
+components:
+  schemas:
+    SomeSchema:
+      type: string
+      default: ` + dflt + `
+`
+	}
+
+	for _, testcase := range []struct {
+		dflt, err string
+	}{
+		{
+			dflt: `"foo"`,
+			err:  "",
+		},
+		{
+			dflt: `1`,
+			err:  "invalid components: invalid schema default: Field must be set to string or not be present",
+		},
+	} {
+		t.Run(testcase.dflt, func(t *testing.T) {
+			spec := specf(testcase.dflt)
+
+			sl := NewLoader()
+
+			doc, err := sl.LoadFromData([]byte(spec))
+			require.NoError(t, err)
+
+			err = doc.Validate(sl.Context)
+			if testcase.err == "" {
+				require.NoError(t, err)
+			} else {
+				require.Error(t, err, testcase.err)
+			}
+		})
+	}
+}
Repository: getkin/kin-openapi
Pull request #630 (2022-10-10 07:41:07)

fix: embedded struct handling
Embedded structs with a json tag name are not nested as would be expected. This fixes this.


Patch:
diff --git a/jsoninfo/field_info.go b/jsoninfo/field_info.go
index 2382b73..6b45f8c 100644
--- a/jsoninfo/field_info.go
+++ b/jsoninfo/field_info.go
@@ -35,11 +35,14 @@ iteration:
 
 		// See whether this is an embedded field
 		if f.Anonymous {
-			if f.Tag.Get("json") == "-" {
+			jsonTag := f.Tag.Get("json")
+			if jsonTag == "-" {
 				continue
 			}
-			fields = AppendFields(fields, index, f.Type)
-			continue iteration
+			if jsonTag == "" {
+				fields = AppendFields(fields, index, f.Type)
+				continue iteration
+			}
 		}
 
 		// Ignore certain types


Test patch:
diff --git a/openapi3gen/openapi3gen_test.go b/openapi3gen/openapi3gen_test.go
index a4c2d52..bfa3120 100644
--- a/openapi3gen/openapi3gen_test.go
+++ b/openapi3gen/openapi3gen_test.go
@@ -18,6 +18,12 @@ import (
 
 func ExampleGenerator_SchemaRefs() {
 	type SomeOtherType string
+	type Embedded struct {
+		Z string `json:"z"`
+	}
+	type Embedded2 struct {
+		A string `json:"a"`
+	}
 	type SomeStruct struct {
 		Bool    bool                      `json:"bool"`
 		Int     int                       `json:"int"`
@@ -38,6 +44,10 @@ func ExampleGenerator_SchemaRefs() {
 			Y string
 		} `json:"structWithoutFields"`
 
+		Embedded `json:"embedded"`
+
+		Embedded2
+
 		Ptr *SomeOtherType `json:"ptr"`
 	}
 
@@ -54,9 +64,12 @@ func ExampleGenerator_SchemaRefs() {
 	}
 	fmt.Printf("schemaRef: %s\n", data)
 	// Output:
-	// g.SchemaRefs: 15
+	// g.SchemaRefs: 16
 	// schemaRef: {
 	//   "properties": {
+	//     "a": {
+	//       "type": "string"
+	//     },
 	//     "bool": {
 	//       "type": "boolean"
 	//     },
@@ -64,6 +77,14 @@ func ExampleGenerator_SchemaRefs() {
 	//       "format": "byte",
 	//       "type": "string"
 	//     },
+	//     "embedded": {
+	//       "properties": {
+	//         "z": {
+	//           "type": "string"
+	//         }
+	//       },
+	//       "type": "object"
+	//     },
 	//     "float64": {
 	//       "format": "double",
 	//       "type": "number"
Repository: getkin/kin-openapi
Pull request #647 (2022-10-24 13:51:59)

Fix validation of complex enum values
Fixes https://github.com/getkin/kin-openapi/issues/646


Patch:
diff --git a/openapi3/schema.go b/openapi3/schema.go
index 641ce85..b38d257 100644
--- a/openapi3/schema.go
+++ b/openapi3/schema.go
@@ -8,6 +8,7 @@ import (
 	"fmt"
 	"math"
 	"math/big"
+	"reflect"
 	"regexp"
 	"sort"
 	"strconv"
@@ -870,7 +871,7 @@ func (schema *Schema) visitJSON(settings *schemaValidationSettings, value interf
 func (schema *Schema) visitSetOperations(settings *schemaValidationSettings, value interface{}) (err error) {
 	if enum := schema.Enum; len(enum) != 0 {
 		for _, v := range enum {
-			if value == v {
+			if reflect.DeepEqual(v, value) {
 				return
 			}
 		}


Test patch:
diff --git a/openapi3/schema_test.go b/openapi3/schema_test.go
index 4c14dcb..abec304 100644
--- a/openapi3/schema_test.go
+++ b/openapi3/schema_test.go
@@ -1314,3 +1314,45 @@ func TestValidationFailsOnInvalidPattern(t *testing.T) {
 	err := schema.Validate(context.Background())
 	require.Error(t, err)
 }
+
+func TestIssue646(t *testing.T) {
+	data := []byte(`
+enum:
+- 42
+- []
+- [a]
+- {}
+- {b: c}
+`[1:])
+
+	var schema Schema
+	err := yaml.Unmarshal(data, &schema)
+	require.NoError(t, err)
+
+	err = schema.Validate(context.Background())
+	require.NoError(t, err)
+
+	err = schema.VisitJSON(42)
+	require.NoError(t, err)
+
+	err = schema.VisitJSON(1337)
+	require.Error(t, err)
+
+	err = schema.VisitJSON([]interface{}{})
+	require.NoError(t, err)
+
+	err = schema.VisitJSON([]interface{}{"a"})
+	require.NoError(t, err)
+
+	err = schema.VisitJSON([]interface{}{"b"})
+	require.Error(t, err)
+
+	err = schema.VisitJSON(map[string]interface{}{})
+	require.NoError(t, err)
+
+	err = schema.VisitJSON(map[string]interface{}{"b": "c"})
+	require.NoError(t, err)
+
+	err = schema.VisitJSON(map[string]interface{}{"d": "e"})
+	require.Error(t, err)
+}