xamarin / AndroidX

AndroidX bindings for .NET for Android
MIT License
173 stars 42 forks source link

AndroidX.Credentials ICredentialManagerCallback wrong parameter types. #856

Open uwe-neuronation opened 3 months ago

uwe-neuronation commented 3 months ago

Android application type

Classic Xamarin.Android (MonoAndroid12.0, etc.), Android for .NET (net6.0-android, etc.)

Affected platform version

VS Mac 2022 17.6.9

Description

The interface ICredentialManagerCallback.cs OnError and OnResult use Java.Lang.Object parameter type. It should be

onResult(GetCredentialResponse result) and onError(GetCredentialException e)

Unfortunately, It is not possible cast. So, it's impossible to get a proper error message on the c# level.

Steps to Reproduce

ICredentialManager credentialManager = CredentialManager.Companion.Create(this.Context);
                CreatePublicKeyCredentialRequest createPublicKeyCredentialRequest = new CreatePublicKeyCredentialRequest(passkeyRequestVO.RequestJson);
                credentialManager.CreateCredentialAsync(this.Context, createPublicKeyCredentialRequest, new Android.OS.CancellationSignal(), this, this);
void ICredentialManagerCallback.OnError(Java.Lang.Object e){   System.Diagnostics.Debug.WriteLine("ICredentialManagerCallback.OnError: " + e);}
void ICredentialManagerCallback.OnResult(Java.Lang.Object result){    System.Diagnostics.Debug.WriteLine("ICredentialManagerCallback.OnResult");}

Bildschirmfoto 2024-03-05 um 11 57 22

Did you find any workaround?

nope

Relevant log output

No response

jpobst commented 3 months ago

You should be able to use JavaCast to cast to the correct type:

void ICredentialManagerCallback.OnResult (Java.Lang.Object result) 
{
    var r = result.JavaCast<GetCredentialResponse> ();
}
uwe-neuronation commented 3 months ago

You should be able to use JavaCast to cast to the correct type:

void ICredentialManagerCallback.OnResult (Java.Lang.Object result) 
{
    var r = result.JavaCast<GetCredentialResponse> ();
}

Thank you @jpobst that works. It's inconvenient, because I can't do type switch. Only works for the explicit exception type. In my case var r = e.JavaCast<CreatePublicKeyCredentialDomException>(); It throws an exception if the type does not match. So to cover all available exceptions types, it will be a big block of code.

Same interface OnResult works better, here it's Java.LangObjcte as well. But I can do a c# cast on the CreatePublicKeyCredentialResponse.

if (result is CreatePublicKeyCredentialResponse cpkcr) { ... }

Something is wrong with the exceptions themself.