Repository: getkin/kin-openapi (go)

Issue:
openapi3gen.NewSchemaRefForValue panics for embedded pointer types
**Summary**

`openapi3gen.NewSchemaRefForValue` panics if it encounters an embedded pointer type when a schema customizer is being used.

See [here](https://go.dev/play/p/A4gXt0Hk1m9) for a minimal example that reproduces the problem.

**Explanation**

I believe the panic is originating [here](https://github.com/getkin/kin-openapi/blob/5352767564ece15c85c3d0a1ea1143d1f4206b04/openapi3gen/openapi3gen.go#L134) in `getStructField`, because `t.Field` panics if the `reflect.Type`'s `Kind` is not `Struct` (and in this case, it's `Ptr`). The panic only occurs when using a schema customizer because `getStructField` is only called (in this case) when a schema customizer is present (see [here](https://github.com/getkin/kin-openapi/blob/5352767564ece15c85c3d0a1ea1143d1f4206b04/openapi3gen/openapi3gen.go#L312-L313)).

I believe this could be addressed by doing something like [this](https://github.com/getkin/kin-openapi/blob/5352767564ece15c85c3d0a1ea1143d1f4206b04/jsoninfo/field_info.go#L24-L26) in [getStructField](https://github.com/getkin/kin-openapi/blob/5352767564ece15c85c3d0a1ea1143d1f4206b04/openapi3gen/openapi3gen.go#L130).

Here's a pull request that implements the fix: https://github.com/getkin/kin-openapi/pull/501.

Pull request description:
issue/500
Fixes #500 by updating [getStructField](https://github.com/nicheinc/kin-openapi/blob/issue/500/openapi3gen/openapi3gen.go#L130-L141) to handle embedded pointer types, which were previously causing panics (but only when a schema customizer was being used). The solution involved adding these [three lines](https://github.com/nicheinc/kin-openapi/blob/issue/500/openapi3gen/openapi3gen.go#L136-L138), to mirror the logic happening [here](https://github.com/getkin/kin-openapi/blob/5352767564ece15c85c3d0a1ea1143d1f4206b04/jsoninfo/field_info.go#L24-L26).

Also includes a new unit test that should help prevent regressions (I verified that the unit test triggered the panic before these changes).

Please let me know if you have any questions!

Patch:
diff --git a/openapi3gen/openapi3gen.go b/openapi3gen/openapi3gen.go
index 45577bc..f2a45c4 100644
--- a/openapi3gen/openapi3gen.go
+++ b/openapi3gen/openapi3gen.go
@@ -133,6 +133,9 @@ func getStructField(t reflect.Type, fieldInfo jsoninfo.FieldInfo) reflect.Struct
 	for i := 0; i < len(fieldInfo.Index); i++ {
 		ff = t.Field(fieldInfo.Index[i])
 		t = ff.Type
+		for t.Kind() == reflect.Ptr {
+			t = t.Elem()
+		}
 	}
 	return ff
 }


Test patch:
diff --git a/openapi3gen/openapi3gen_test.go b/openapi3gen/openapi3gen_test.go
index a6d1620..3640d4c 100644
--- a/openapi3gen/openapi3gen_test.go
+++ b/openapi3gen/openapi3gen_test.go
@@ -294,6 +294,42 @@ func TestEmbeddedPointerStructs(t *testing.T) {
 	require.Equal(t, true, ok)
 }
 
+// See: https://github.com/getkin/kin-openapi/issues/500
+func TestEmbeddedPointerStructsWithSchemaCustomizer(t *testing.T) {
+	type EmbeddedStruct struct {
+		ID string
+	}
+
+	type ContainerStruct struct {
+		Name string
+		*EmbeddedStruct
+	}
+
+	instance := &ContainerStruct{
+		Name: "Container",
+		EmbeddedStruct: &EmbeddedStruct{
+			ID: "Embedded",
+		},
+	}
+
+	customizerFn := func(name string, t reflect.Type, tag reflect.StructTag, schema *openapi3.Schema) error {
+		return nil
+	}
+	customizerOpt := openapi3gen.SchemaCustomizer(customizerFn)
+
+	generator := openapi3gen.NewGenerator(openapi3gen.UseAllExportedFields(), customizerOpt)
+
+	schemaRef, err := generator.GenerateSchemaRef(reflect.TypeOf(instance))
+	require.NoError(t, err)
+
+	var ok bool
+	_, ok = schemaRef.Value.Properties["Name"]
+	require.Equal(t, true, ok)
+
+	_, ok = schemaRef.Value.Properties["ID"]
+	require.Equal(t, true, ok)
+}
+
 func TestCyclicReferences(t *testing.T) {
 	type ObjectDiff struct {
 		FieldCycle *ObjectDiff