Skip to content
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

GetEvents GetEventsParams.Type array parameter is not handled by GetQueryParams function #381

Open
gagf01 opened this issue Oct 26, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@gagf01
Copy link

gagf01 commented Oct 26, 2022

Describe the bug
When the GetEventsParams.Type parameter is passed to the GetEvents function, it generate an error because the GetQueryParams function can't handle arrays. It should transform them first to a comma separated list string before the json.Mashal call.

To Reproduce

GetEvents(
  ctx,
  c.Token.AccessToken,
  c.Realm,
  gocloak.GetEventsParams{
	  UserID: "some-user-id",
	  Type:   []string{"LOGIN"},
  })

// GetEvents : could not get events: json: cannot unmarshal array into Go value of type string
@Nerzal Nerzal added the bug Something isn't working label Oct 26, 2022
@michael87
Copy link

+1
I have the same issue when i do this:

GetEvents( ctx, c.Token.AccessToken, c.Realm, gocloak.GetEventsParams{ UserID: "some-user-id", First: 10, })

of course in the int32 format.

@StinkyPeach
Copy link

+2
I have the same issue when i do this:

client.GetEvents(context.Background(), jwt.AccessToken, KeycloakRealm, gocloak.GetEventsParams{
		UserID: gocloak.StringP("user_id"),
		Type: []string{"LOGIN"},
type GetEventsParams struct {
	Client    *string  `json:"client,omitempty"`
	DateFrom  *string  `json:"dateFrom,omitempty"`
	DateTo    *string  `json:"dateTo,omitempty"`
	First     *int32   `json:"first,string,omitempty"`
	IPAddress *string  `json:"ipAddress,omitempty"`
	Max       *int32   `json:"max,string,omitempty"`
	Type      []string `json:"type,omitempty"`
	UserID    *string  `json:"user,omitempty"`
}

func GetQueryParams(s interface{}) (map[string]string, error) {
	// if obj, ok := s.(GetGroupsParams); ok {
	// 	obj.OnMarshal()
	// 	s = obj
	// }
	b, err := json.Marshal(s)
	if err != nil {
		return nil, err
	}
	var res map[string]string
	err = json.Unmarshal(b, &res)
	if err != nil {
		return nil, err
	}
	return res, nil
}

The GetQueryParams function returns a map[string]string, but the type field of the GetEventsParams structure is []string.

@pla2aroi
Copy link

pla2aroi commented Apr 28, 2023

solution solved

create a new function GetEvents

res, err := GetEvents(ctx, gocloak.GetEventsParams{First: 10}, []string{"LOGIN", "LOGIN_ERROR"})

...

func GetEvents(ctx context.Context, params gocloak.GetEventsParams, paramTypes []string) (res []*gocloak.EventRepresentation, err error) {
	
        ...

	if len(paramTypes) > 0 {
		client.RestyClient().OnBeforeRequest(func(c *resty.Client, r *resty.Request) error {
			r.SetQueryParamsFromValues(url.Values{
				"type": paramTypes,
			})
			return nil
		})
	}

	return client.GetEvents(ctx, token, "realm-name", params)
}

@valerii15298
Copy link

@Nerzal any info if this will be addressed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants