Fix return of primitive types by removing explicit cast, add tests to prevent regressions