From a21b0ea904738b24b47d32707ec7600248eec842 Mon Sep 17 00:00:00 2001 From: Tamas Berki Date: Thu, 22 May 2025 01:10:58 +0200 Subject: [PATCH 1/2] openapi: fix missing request schema for custom plugins github.com/mitchellh/mapstructure does a strings.EqualFold check between a struct key and a map key. This does not work for $ref key, a custom name match is needed to cover this edge case. Resolves: #30714 Signed-off-by: Tamas Berki --- sdk/framework/openapi.go | 10 ++++++++ sdk/framework/openapi_test.go | 46 +++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/sdk/framework/openapi.go b/sdk/framework/openapi.go index 3a232537b1..4f5fc3a856 100644 --- a/sdk/framework/openapi.go +++ b/sdk/framework/openapi.go @@ -64,10 +64,20 @@ func NewOASDocumentFromMap(input map[string]interface{}) (*OASDocument, error) { return inputRaw, nil } + // mapstructure does a strings.EqualFold check between a struct key and a map key. + // This does not work for $ref key, a custom match is needed to cover this edge case. + matchName := func(mapKey, fieldName string) bool { + if strings.HasPrefix(mapKey, "$") { + return strings.EqualFold(mapKey[1:], fieldName) + } + return strings.EqualFold(mapKey, fieldName) + } + doc := new(OASDocument) config := &mapstructure.DecoderConfig{ DecodeHook: decodeHook, + MatchName: matchName, Result: doc, } diff --git a/sdk/framework/openapi_test.go b/sdk/framework/openapi_test.go index cb0c62445b..26bb468e23 100644 --- a/sdk/framework/openapi_test.go +++ b/sdk/framework/openapi_test.go @@ -744,6 +744,52 @@ func TestOpenAPI_CustomDecoder(t *testing.T) { } } +func TestOpenAPI_CustomNameMatcher(t *testing.T) { + p := &Path{ + Pattern: "foo", + HelpSynopsis: "Synopsis", + Fields: map[string]*FieldSchema{ + "foo": { + Type: TypeString, + Description: "foo description", + }, + "bar": { + Type: TypeString, + Description: "bar description", + }, + }, + Operations: map[logical.Operation]OperationHandler{ + logical.UpdateOperation: &PathOperation{ + Summary: "My Summary", + }, + }, + } + + docOrig := NewOASDocument("version") + err := documentPath(p, &Backend{BackendType: logical.TypeLogical}, "kv", docOrig) + if err != nil { + t.Fatal(err) + } + + docJSON := mustJSONMarshal(t, docOrig) + + var intermediate map[string]interface{} + if err := jsonutil.DecodeJSON(docJSON, &intermediate); err != nil { + t.Fatal(err) + } + + docNew, err := NewOASDocumentFromMap(intermediate) + if err != nil { + t.Fatal(err) + } + + docNewJSON := mustJSONMarshal(t, docNew) + + if diff := deep.Equal(docJSON, docNewJSON); diff != nil { + t.Fatal(diff) + } +} + func TestOpenAPI_CleanResponse(t *testing.T) { // Verify that an all-null input results in empty JSON orig := &logical.Response{} From 0023c73655d9eb3dfc87b47e94f8a6c4f309e2de Mon Sep 17 00:00:00 2001 From: Tamas Berki Date: Thu, 22 May 2025 01:17:33 +0200 Subject: [PATCH 2/2] docs: Add changelog Signed-off-by: Tamas Berki --- changelog/30715.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelog/30715.txt diff --git a/changelog/30715.txt b/changelog/30715.txt new file mode 100644 index 0000000000..bc42839685 --- /dev/null +++ b/changelog/30715.txt @@ -0,0 +1,3 @@ +```release-note:bug +openapi: fix missing request schema for custom plugins +```