-
Notifications
You must be signed in to change notification settings - Fork 6
Fix/create identity identity id decode #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
BrandtKruger
wants to merge
4
commits into
kinde-oss:main
from
BrandtKruger:fix/create-identity-identity-id-decode
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ab41550
feat: add invitation code support to authentication flow
BrandtKruger 62cfbce
fix(management_api): decode CreateIdentityResponse identity_id from API
BrandtKruger 21e2556
ci: apply CreateIdentity identity_id patch before tests
BrandtKruger c80f5ee
ci: run identity_id patch before tests; fail fast if pattern missing;…
BrandtKruger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package management_api | ||
|
|
||
| // CreateIdentity response decode tests. The decoder is patched by fix_create_identity_identity.go | ||
| // to accept "identity_id" from the API; CI runs that patch before tests (see .github/workflows/ci.yml). | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/go-faster/jx" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestCreateIdentityResponse_Decode_WithId verifies that a response with "id" field decodes correctly. | ||
| func TestCreateIdentityResponse_Decode_WithId(t *testing.T) { | ||
| // Standard API response with "id" field | ||
| json := `{ | ||
| "message": "Identity created", | ||
| "code": "IDENTITY_CREATED", | ||
| "identity": { | ||
| "id": "idl_abc123" | ||
| } | ||
| }` | ||
|
|
||
| d := jx.DecodeBytes([]byte(json)) | ||
| var response CreateIdentityResponse | ||
|
|
||
| err := response.Decode(d) | ||
| require.NoError(t, err) | ||
|
|
||
| assert.True(t, response.Identity.IsSet(), "Identity should be set") | ||
| identity, ok := response.Identity.Get() | ||
| require.True(t, ok) | ||
| assert.True(t, identity.ID.IsSet(), "Identity ID should be set") | ||
| id, ok := identity.ID.Get() | ||
| require.True(t, ok) | ||
| assert.Equal(t, "idl_abc123", id, "Identity ID should be decoded correctly") | ||
| } | ||
|
|
||
| // TestCreateIdentityResponse_Decode_WithIdentityId verifies that when the API returns | ||
| // "identity_id" (e.g. for existing enterprise identity), the patched decoder maps it to ID. | ||
| func TestCreateIdentityResponse_Decode_WithIdentityId(t *testing.T) { | ||
| // API response when creating identity with existing enterprise value - returns "identity_id" not "id" | ||
| json := `{ | ||
| "message": "Identity created", | ||
| "code": "IDENTITY_CREATED", | ||
| "identity": { | ||
| "identity_id": "idl_existing_enterprise_123" | ||
| } | ||
| }` | ||
|
|
||
| d := jx.DecodeBytes([]byte(json)) | ||
| var response CreateIdentityResponse | ||
|
|
||
| err := response.Decode(d) | ||
| require.NoError(t, err) | ||
|
|
||
| assert.True(t, response.Identity.IsSet(), "Identity should be set") | ||
| identity, ok := response.Identity.Get() | ||
| require.True(t, ok) | ||
| assert.True(t, identity.ID.IsSet(), "Identity ID should be set (from identity_id field)") | ||
| id, ok := identity.ID.Get() | ||
| require.True(t, ok) | ||
| assert.Equal(t, "idl_existing_enterprise_123", id, "Identity ID should be decoded from identity_id field") | ||
| } | ||
|
|
||
| // TestCreateIdentityResponseIdentity_Decode_IdentityIdField verifies the identity object | ||
| // decoder accepts "identity_id" and populates ID (for API compatibility). | ||
| func TestCreateIdentityResponseIdentity_Decode_IdentityIdField(t *testing.T) { | ||
| json := `{"identity_id": "idl_xyz789"}` | ||
|
|
||
| d := jx.DecodeBytes([]byte(json)) | ||
| var identity CreateIdentityResponseIdentity | ||
|
|
||
| err := identity.Decode(d) | ||
| require.NoError(t, err) | ||
|
|
||
| assert.True(t, identity.ID.IsSet(), "ID should be set from identity_id field") | ||
| id, ok := identity.ID.Get() | ||
| require.True(t, ok) | ||
| assert.Equal(t, "idl_xyz789", id) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| //go:build ignore | ||
| // +build ignore | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "os" | ||
| ) | ||
|
|
||
| // This tool patches the generated oas_json_gen.go so that CreateIdentityResponseIdentity.Decode() | ||
| // accepts the "identity_id" field returned by the Kinde API (e.g. when creating identity with | ||
| // existing enterprise identity). The OpenAPI schema uses "id" but the API may return "identity_id". | ||
|
|
||
| const ( | ||
| targetFile = "oas_json_gen.go" | ||
| ) | ||
|
|
||
| // Exact block that appears only in CreateIdentityResponseIdentity.Decode (full ObjBytes callback | ||
| // including the unique wrap "decode CreateIdentityResponseIdentity" so we match exactly once). | ||
| var oldBlock = []byte(` if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { | ||
| switch string(k) { | ||
| case "id": | ||
| if err := func() error { | ||
| s.ID.Reset() | ||
| if err := s.ID.Decode(d); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| }(); err != nil { | ||
| return errors.Wrap(err, "decode field \"id\"") | ||
| } | ||
| default: | ||
| return d.Skip() | ||
| } | ||
| return nil | ||
| }); err != nil { | ||
| return errors.Wrap(err, "decode CreateIdentityResponseIdentity") | ||
| }`) | ||
|
|
||
| // Same block with identity_id case inserted before default. | ||
| var newBlock = []byte(` if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { | ||
| switch string(k) { | ||
| case "id": | ||
| if err := func() error { | ||
| s.ID.Reset() | ||
| if err := s.ID.Decode(d); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| }(); err != nil { | ||
| return errors.Wrap(err, "decode field \"id\"") | ||
| } | ||
| case "identity_id": | ||
| // API returns identity_id (e.g. for existing enterprise identity); map to ID for compatibility. | ||
| if err := func() error { | ||
| s.ID.Reset() | ||
| if err := s.ID.Decode(d); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| }(); err != nil { | ||
| return errors.Wrap(err, "decode field \"identity_id\"") | ||
| } | ||
| default: | ||
| return d.Skip() | ||
| } | ||
| return nil | ||
| }); err != nil { | ||
| return errors.Wrap(err, "decode CreateIdentityResponseIdentity") | ||
| }`) | ||
|
|
||
| func main() { | ||
| fmt.Printf("Patching %s for CreateIdentityResponseIdentity identity_id...\n", targetFile) | ||
|
|
||
| content, err := os.ReadFile(targetFile) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error reading file: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| // Only replace the first occurrence (CreateIdentityResponseIdentity is the only decoder with this exact block). | ||
| if bytes.Contains(content, []byte("case \"identity_id\":\n\t\t\t// API returns identity_id")) { | ||
| fmt.Println("Already patched - identity_id case present") | ||
| os.Exit(0) | ||
| } | ||
|
|
||
| count := bytes.Count(content, oldBlock) | ||
| if count == 0 { | ||
| fmt.Println("Pattern not found - generator output may have changed") | ||
| fmt.Println("Please verify CreateIdentityResponseIdentity.Decode manually") | ||
| os.Exit(1) | ||
| } | ||
|
BrandtKruger marked this conversation as resolved.
|
||
| if count > 1 { | ||
| fmt.Fprintf(os.Stderr, "Pattern matched %d times; expected 1 (CreateIdentityResponseIdentity). Refusing to patch.\n", count) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| newContent := bytes.Replace(content, oldBlock, newBlock, 1) | ||
| if bytes.Equal(content, newContent) { | ||
| fmt.Println("No changes made") | ||
| os.Exit(0) | ||
| } | ||
|
|
||
| if err := os.WriteFile(targetFile, newContent, 0644); err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error writing file: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| fmt.Println("Successfully patched CreateIdentityResponseIdentity.Decode() for identity_id") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.