casdoor / casdoor-go-sdk

Go client SDK for Casdoor
https://github.com/casdoor/casdoor
Apache License 2.0
82 stars 65 forks source link

Unable to unmarshal response data after Casdoor using RespnseOk #71

Closed wintbiit closed 11 months ago

wintbiit commented 11 months ago

after https://github.com/casdoor/casdoor/commit/a6f803aff1e634ae92b669975baa366a6b3b39c8, casdoor-go-sdk codes like

func GetUser(name string) (*User, error) {
    queryMap := map[string]string{
        "id": fmt.Sprintf("%s/%s", authConfig.OrganizationName, name),
    }

    url := GetUrl("get-user", queryMap)

    bytes, err := DoGetBytesRaw(url)
    if err != nil {
        return nil, err
    }

    var user *User
    err = json.Unmarshal(bytes, &user)
    if err != nil {
        return nil, err
    }
    return user, nil
}

No longer works (return empty struct / panic) because response format has changed from data model alone to wrapped response json. Take the user.go#GetUser() above as example.

Before https://github.com/casdoor/casdoor/commit/a6f803aff1e634ae92b669975baa366a6b3b39c8 /api/get-user returns just user modal.

image

Then after it returns standard reponse modal in which user modal is wrapped in data field.

image

Quite a lot apis in casdoor-go-sdk has been influenced, such as user.go, resource.go...... Almost anywhere code like

bytes, err := DoGetBytesRaw(url)
if err != nil {
    return nil, err
}

var user *User
err = json.Unmarshal(bytes, &user)

encounters this.

By the way I noticed that actually DoGetBytesRaw() already unmarshaled response bytes to standard response data modal and then return the original reponse bytes. Maybe changing this would help.

// DoGetBytesRaw is a general function to get response from param url through HTTP Get method.
func DoGetBytesRaw(url string) ([]byte, error) {
    respBytes, err := doGetBytesRawWithoutCheck(url)
    if err != nil {
        return nil, err
    }

    var response Response
    err = json.Unmarshal(respBytes, &response)
    if err == nil && response.Status == "error"{
        return nil, errors.New(response.Msg)
    }

    return respBytes, nil
}

Maybe more sdks like java-sdk, js-sdk or rust-sdk also have this issue, are the next steps of https://github.com/casdoor/casdoor/commit/a6f803aff1e634ae92b669975baa366a6b3b39c8 underway?

casbin-bot commented 11 months ago

@seriouszyx @ComradeProgrammer @Resulte

hsluoyz commented 11 months ago

@WinterOfBit yes, all SDKs need to be updated after PR: https://github.com/casdoor/casdoor/pull/2111 is merged. Can you update the Go SDK?

wintbiit commented 11 months ago

@WinterOfBit yes, all SDKs need to be updated after PR: casdoor/casdoor#2111 is merged. Can you update the Go SDK?

Ok. working on this now.

hsluoyz commented 11 months ago

@WinterOfBit the PR: https://github.com/casdoor/casdoor/pull/2111 was already merged. Can you proceed with this issue now?

wintbiit commented 11 months ago

@WinterOfBit the PR: casdoor/casdoor#2111 was already merged. Can you proceed with this issue now?

@hsluoyz #73 altered all usages of base.go#DoGetBytesRaw() with base.go#DoGetBytes(), which seems to have solved this issue. Am I missing any APIs?

hsluoyz commented 11 months ago

@WinterOfBit ok, then plz close this issue if resolved