Skip to content

Commit

Permalink
Fix OpenAPI validation by deep-copying method struct and prevent modi…
Browse files Browse the repository at this point in the history
…dying previous object.
  • Loading branch information
apognu committed Feb 19, 2025
1 parent 6345a50 commit f2f9928
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions dto/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/checkmarble/marble-backend/models"
"github.com/checkmarble/marble-backend/utils"
"github.com/mitchellh/copystructure"
)

type Info struct {
Expand Down Expand Up @@ -36,7 +37,7 @@ type Property struct {
}

type Schema struct {
Ref string `json:"$ref"`
Ref string `json:"$ref,omitempty"`
Type string `json:"type,omitempty"`
Items *Schema `json:"items,omitempty"`
MaxItems int `json:"maxItems,omitempty"`
Expand Down Expand Up @@ -492,7 +493,7 @@ func OpenAPIFromDataModel(dataModel models.DataModel) Reference {
}
ref.Paths[fmt.Sprintf("/ingestion/%s", table.Name)] = object

methodObj = MethodObject{
methodObjMultiple := MethodObject{
Security: []map[string][]string{
{
"api_key": []string{},
Expand Down Expand Up @@ -536,13 +537,16 @@ func OpenAPIFromDataModel(dataModel models.DataModel) Reference {
},
},
}
methodObjPatch = methodObj
methodObjPatch.RequestBody.Content.ApplicationJSON.Schema.Ref =
methodObjPatchMultipleAny, _ := copystructure.Copy(methodObjMultiple)
methodObjPatchMultiple := methodObjPatchMultipleAny.(MethodObject)
methodObjPatchMultiple.RequestBody.Content.ApplicationJSON.Schema.Items.Ref =
fmt.Sprintf("#/components/schemas/%s-partial", table.Name)

object = PathObject{
Post: &methodObj,
Patch: &methodObjPatch,
Post: &methodObjMultiple,
Patch: &methodObjPatchMultiple,
}

ref.Paths[fmt.Sprintf("/ingestion/%s/multiple", table.Name)] = object
}

Expand Down

0 comments on commit f2f9928

Please sign in to comment.