From e13f484bcbacb8830afe9aa7ad4fb77ee3efbaed Mon Sep 17 00:00:00 2001 From: Samuel Lorch Date: Fri, 24 Nov 2023 13:32:47 +0100 Subject: [PATCH] Add Workaround for inconsistent API Response --- api/resource_types.go | 12 ++++++------ helper/util.go | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/api/resource_types.go b/api/resource_types.go index 0f091d0..3bdc1bc 100644 --- a/api/resource_types.go +++ b/api/resource_types.go @@ -8,12 +8,12 @@ import ( // ResourceType is the Type of a Resource type ResourceType struct { - ID string `json:"id,omitempty"` - Slug string `json:"slug,omitempty"` - Description string `json:"description,omitempty"` - Definition string `json:"definition,omitempty"` - Created *Time `json:"created,omitempty"` - Modified *Time `json:"modified,omitempty"` + ID string `json:"id,omitempty"` + Slug string `json:"slug,omitempty"` + Description string `json:"description,omitempty"` + Definition json.RawMessage `json:"definition,omitempty"` + Created *Time `json:"created,omitempty"` + Modified *Time `json:"modified,omitempty"` } type ResourceTypeSchema struct { diff --git a/helper/util.go b/helper/util.go index 1692870..4d7a144 100644 --- a/helper/util.go +++ b/helper/util.go @@ -41,7 +41,22 @@ func validateSecretData(rType *api.ResourceType, secretData string) error { var schemaDefinition api.ResourceTypeSchema err := json.Unmarshal([]byte(rType.Definition), &schemaDefinition) if err != nil { - return fmt.Errorf("Unmarshal Json Schema: %w", err) + // Workaround for inconsistant API Responses where sometime the Schema is embedded directly and sometimes it's escaped as a string + if err.Error() == "json: cannot unmarshal string into Go value of type api.ResourceTypeSchema" { + var tmp string + err = json.Unmarshal([]byte(rType.Definition), &tmp) + if err != nil { + return fmt.Errorf("Workaround Unmarshal Json Schema String: %w", err) + } + + err = json.Unmarshal([]byte(tmp), &schemaDefinition) + if err != nil { + return fmt.Errorf("Workaround Unmarshal Json Schema: %w", err) + } + + } else { + return fmt.Errorf("Unmarshal Json Schema: %w", err) + } } comp := jsonschema.NewCompiler()