Skip to content

Commit

Permalink
Add Workaround for inconsistent API Response
Browse files Browse the repository at this point in the history
  • Loading branch information
speatzle committed Nov 24, 2023
1 parent 360cc37 commit e13f484
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
12 changes: 6 additions & 6 deletions api/resource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 16 additions & 1 deletion helper/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit e13f484

Please sign in to comment.