FusionAuth / go-client

FusionAuth Go Client Library!
https://fusionauth.io/
Apache License 2.0
29 stars 32 forks source link

RegistrationRequest.User omitempty ineffective #77

Open andrius4669 opened 10 months ago

andrius4669 commented 10 months ago

hello. when i use RegisterWithContext func for the purpose of https://fusionauth.io/docs/v1/tech/apis/registrations#create-a-user-registration-for-an-existing-user such as

    res, errs, err := faClient.RegisterWithContext(
        ctx, userID, fusionauth.RegistrationRequest{
            GenerateAuthenticationToken: true,
            Registration: fusionauth.UserRegistration{
                ApplicationId: applicationID,
                Id:            rID,
                Username:      uname,
            },
            SkipRegistrationVerification: true,
        })

it always fails with errs.FieldErrors of map[user.email:[{Code:[blank]user.email Data:map[] Message:You must specify either the [user.email] or [user.username] property. If you are emailing the user you must specify the [user.email].}] user.password:[{Code:[blank]user.password Data:map[] Message:You must specify the [user.password] property.}] user.username:[{Code:[blank]user.username Data:map[] Message:You must specify either the [user.email] or [user.username] property. If you are emailing the user you must specify the [user.email].}] userId:[{Code:[duplicate]userId Data:map[] Message:A User with Id [2bc3b58e-762a-40f8-b4cf-6dcc99ec09c8] already exists.}]] which basically means that it simultaneously complains about user.email/user.password/user.username being empty and userId already existing (not a surprise, I didn't intend to create a new user). however, when I do:

type faReg struct {
    DisableDomainBlock           bool                        `json:"disableDomainBlock"`
    GenerateAuthenticationToken  bool                        `json:"generateAuthenticationToken"`
    Registration                 fusionauth.UserRegistration `json:"registration,omitempty"`
    SendSetPasswordEmail         bool                        `json:"sendSetPasswordEmail"`
    SkipRegistrationVerification bool                        `json:"skipRegistrationVerification"`
    SkipVerification             bool                        `json:"skipVerification"`
}

func faDoReg(faClient *fusionauth.FusionAuthClient, ctx context.Context, userId string, request faReg) (*fusionauth.RegistrationResponse, *fusionauth.Errors, error) {
    var resp fusionauth.RegistrationResponse
    var errors fusionauth.Errors

    restClient := faClient.Start(&resp, &errors)
    err := restClient.WithUri("/api/user/registration").
        WithUriSegment(userId).
        WithJSONBody(request).
        WithMethod(http.MethodPost).
        Do(ctx)
    if restClient.ErrorRef == nil {
        return &resp, nil, err
    }
    return &resp, &errors, err
}

//...
    res, errs, err := faDoReg(
        faClient, ctx, userID, faReg{
            GenerateAuthenticationToken: true,
            Registration: fusionauth.UserRegistration{
                ApplicationId: applicationID,
                Id:            rID,
                Username:      uname,
            },
            SkipRegistrationVerification: true,
        })
//...

it works without issues, because User field was not included.

please consider making fusionauth.RegistrationRequest User field a pointer type so that omitempty works, as it is ineffective for structs, and the existence of omitempty here means that it was intended to be omittable.

mooreds commented 10 months ago

Thanks. I think we have a pull request open here: https://github.com/FusionAuth/go-client/pull/61 which does a similar thing. It's been open for a while :( but we haven't had a chance to fully test the change, so haven't incorporated it.