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

expose jwt package #62

Merged
merged 8 commits into from
Dec 1, 2023
Merged

Conversation

samtuga1
Copy link
Contributor

@samtuga1 samtuga1 commented Nov 30, 2023

Resolves #60

Description

Turns out jsonEncode used in res.json cannot encode DateTime(is not serializable).
So I created a custom encoder to encode DateTime but whenever we find out about something else
then we add it for now

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

@samtuga1 samtuga1 changed the title added a custom encoder and exposed jwt package added a custom encoder and exposed jwt package fixes #60 Nov 30, 2023
@samtuga1 samtuga1 changed the title added a custom encoder and exposed jwt package fixes #60 added a custom encoder and exposed jwt package closes#60 Nov 30, 2023
@samtuga1 samtuga1 changed the title added a custom encoder and exposed jwt package closes#60 added a custom encoder and exposed jwt package KEYWORD #60 Nov 30, 2023
@samtuga1 samtuga1 changed the title added a custom encoder and exposed jwt package KEYWORD #60 added a custom encoder and exposed jwt package Nov 30, 2023
@codekeyz codekeyz changed the title added a custom encoder and exposed jwt package added a custom encoder and expose jwt package Dec 1, 2023
@codekeyz codekeyz self-requested a review December 1, 2023 00:28
@codekeyz codekeyz added the bug Something isn't working label Dec 1, 2023
Copy link
Owner

@codekeyz codekeyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome stuff @samtuga1. Tho we'll need to relook at a few stuffs.

packages/pharaoh/lib/src/http/response.dart Outdated Show resolved Hide resolved
packages/pharaoh/lib/src/http/response.dart Outdated Show resolved Hide resolved
@codekeyz
Copy link
Owner

codekeyz commented Dec 1, 2023

Looks like the CI is failing for some Dart Analyze issues. @samtuga1

@samtuga1
Copy link
Contributor Author

samtuga1 commented Dec 1, 2023

Looks like the CI is failing for some Dart Analyze issues. @samtuga1

$ melos exec
  └> dart analyze . --fatal-infos
     └> FAILED (in 1 packages)
        └> pharaoh_jwt_auth (with exit code 1)

melos run analyze
  └> melos exec -c 1 -- \
       dart analyze . --fatal-infos
     └> FAILED
ScriptException: The script analyze failed to execute.

Do you know what is happening? maybe its coming from the export i did

@samtuga1
Copy link
Contributor Author

samtuga1 commented Dec 1, 2023

Looks like the CI is failing for some Dart Analyze issues. @samtuga1

$ melos exec
  └> dart analyze . --fatal-infos
     └> FAILED (in 1 packages)
        └> pharaoh_jwt_auth (with exit code 1)

melos run analyze
  └> melos exec -c 1 -- \
       dart analyze . --fatal-infos
     └> FAILED
ScriptException: The script analyze failed to execute.

Do you know what is happening? maybe its coming from the export i did

@codekeyz check this out invertase/melos#303 (comment)

@codekeyz
Copy link
Owner

codekeyz commented Dec 1, 2023

Okay. Taking a look. Give me a moment

@codekeyz
Copy link
Owner

codekeyz commented Dec 1, 2023

Looks like the CI is failing for some Dart Analyze issues. @samtuga1

$ melos exec
  └> dart analyze . --fatal-infos
     └> FAILED (in 1 packages)
        └> pharaoh_jwt_auth (with exit code 1)

melos run analyze
  └> melos exec -c 1 -- \
       dart analyze . --fatal-infos
     └> FAILED
ScriptException: The script analyze failed to execute.

Do you know what is happening? maybe its coming from the export i did

@codekeyz check this out invertase/melos#303 (comment)

CI Log

This is the issue. @samtuga1 Aren't you able to view the CI-logs on here?

@codekeyz
Copy link
Owner

codekeyz commented Dec 1, 2023

You left an unused import in the JWT Example project @samtuga1

@samtuga1
Copy link
Contributor Author

samtuga1 commented Dec 1, 2023

You left an unused import in the JWT Example project @samtuga1

should be fixed now

@samtuga1
Copy link
Contributor Author

samtuga1 commented Dec 1, 2023

Looks like the CI is failing for some Dart Analyze issues. @samtuga1

$ melos exec
  └> dart analyze . --fatal-infos
     └> FAILED (in 1 packages)
        └> pharaoh_jwt_auth (with exit code 1)

melos run analyze
  └> melos exec -c 1 -- \
       dart analyze . --fatal-infos
     └> FAILED
ScriptException: The script analyze failed to execute.

Do you know what is happening? maybe its coming from the export i did

@codekeyz check this out invertase/melos#303 (comment)

CI Log

This is the issue. @samtuga1 Aren't you able to view the CI-logs on here?

It totally skipped me...did not check the top

@codekeyz
Copy link
Owner

codekeyz commented Dec 1, 2023

There's another unused import in a test file. @samtuga1 See Here: https://github.com/codekeyz/pharaoh/actions/runs/7054414617/job/19203239631?pr=62

@samtuga1
Copy link
Contributor Author

samtuga1 commented Dec 1, 2023

There's another unused import in a test file. @samtuga1 See Here: https://github.com/codekeyz/pharaoh/actions/runs/7054414617/job/19203239631?pr=62

There's another unused import in a test file. @samtuga1 See Here: https://github.com/codekeyz/pharaoh/actions/runs/7054414617/job/19203239631?pr=62

There's another unused import in a test file. @samtuga1 See Here: https://github.com/codekeyz/pharaoh/actions/runs/7054414617/job/19203239631?pr=62

There's another unused import in a test file. @samtuga1 See Here: https://github.com/codekeyz/pharaoh/actions/runs/7054414617/job/19203239631?pr=62

I dont know how those imports are appearing in the first place but should be fixed now 👍🏽

@samtuga1 samtuga1 requested a review from codekeyz December 1, 2023 01:25
Copy link
Owner

@codekeyz codekeyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI is failing again. I think something broke somewhere. Please try resolve, ping me if you need assistance. @samtuga1

@samtuga1
Copy link
Contributor Author

samtuga1 commented Dec 1, 2023

CI is failing again. I think something broke somewhere. Please try resolve, ping me if you need assistance. @samtuga1

Seems like its coming from the test

❌ ./test/middleware/session_mw_test.dart: session_middleware should create a new session (failed)
  Expected: <200>
    Actual: <500>
  
  package:matcher                                 expect
  package:spookie/src/http_expectation.dart 81:7  HttpResponseExpection.test
  ===== asynchronous gap ===========================
  test/middleware/session_mw_test.dart 144:7      main.<fn>.<fn>
✅ ./test/middleware/cookie_parser_test.dart: cookieParser when cookies are sent should populate req.cookies

Something is happening there

@codekeyz
Copy link
Owner

codekeyz commented Dec 1, 2023

CI is failing again. I think something broke somewhere. Please try resolve, ping me if you need assistance. @samtuga1

Seems like its coming from the test

❌ ./test/middleware/session_mw_test.dart: session_middleware should create a new session (failed)
  Expected: <200>
    Actual: <500>
  
  package:matcher                                 expect
  package:spookie/src/http_expectation.dart 81:7  HttpResponseExpection.test
  ===== asynchronous gap ===========================
  test/middleware/session_mw_test.dart 144:7      main.<fn>.<fn>
✅ ./test/middleware/cookie_parser_test.dart: cookieParser when cookies are sent should populate req.cookies

Something is happening there

@samtuga1 It's prolly something to do with your changes. Because Session has a some related DateTime info i think. Please verify in the test that is failing to be sure what is happening.

@codekeyz
Copy link
Owner

codekeyz commented Dec 1, 2023

CI is failing again. I think something broke somewhere. Please try resolve, ping me if you need assistance. @samtuga1

Seems like its coming from the test

❌ ./test/middleware/session_mw_test.dart: session_middleware should create a new session (failed)
  Expected: <200>
    Actual: <500>
  
  package:matcher                                 expect
  package:spookie/src/http_expectation.dart 81:7  HttpResponseExpection.test
  ===== asynchronous gap ===========================
  test/middleware/session_mw_test.dart 144:7      main.<fn>.<fn>
✅ ./test/middleware/cookie_parser_test.dart: cookieParser when cookies are sent should populate req.cookies

Something is happening there

@samtuga1 It's prolly something to do with your changes. Because Session has a some related DateTime info i think. Please verify in the test that is failing to be sure what is happening.

Oh right. Something is wrong with the jsonEncode.

    test('should create a new session', () async {
      final app = Pharaoh()
          .use(session(secret: 'foo bar baz'))
          .get('/', (req, res) => res.json(req.session)); // <----- this is failing

This is the test that is failing. @samtuga1

@samtuga1
Copy link
Contributor Author

samtuga1 commented Dec 1, 2023

CI is failing again. I think something broke somewhere. Please try resolve, ping me if you need assistance. @samtuga1

Seems like its coming from the test

❌ ./test/middleware/session_mw_test.dart: session_middleware should create a new session (failed)
  Expected: <200>
    Actual: <500>
  
  package:matcher                                 expect
  package:spookie/src/http_expectation.dart 81:7  HttpResponseExpection.test
  ===== asynchronous gap ===========================
  test/middleware/session_mw_test.dart 144:7      main.<fn>.<fn>
✅ ./test/middleware/cookie_parser_test.dart: cookieParser when cookies are sent should populate req.cookies

Something is happening there

@samtuga1 It's prolly something to do with your changes. Because Session has a some related DateTime info i think. Please verify in the test that is failing to be sure what is happening.

Oh right. Something is wrong with the jsonEncode.

    test('should create a new session', () async {
      final app = Pharaoh()
          .use(session(secret: 'foo bar baz'))
          .get('/', (req, res) => res.json(req.session)); // <----- this is failing

This is the test that is failing. @samtuga1

why are you not doing session.toJson() I can see you have a toJson method. Right now the type is not Datetime but its an Instance of type Session

@codekeyz
Copy link
Owner

codekeyz commented Dec 1, 2023

I think your change is altering how jsonEncode works.

If toEncodable is omitted, it defaults to a function that returns the result of calling .toJson() on the unencodable object.

See here: https://api.flutter.dev/flutter/dart-convert/jsonEncode.html

@codekeyz
Copy link
Owner

codekeyz commented Dec 1, 2023

I think you should leave it as it. Any object being passed to res.json(Object) should either be serializable or have a .toJson() method.

If it fails, that's not our responsibility.

What we can do is add some comment to the res.json method to set expectations. @samtuga1

@samtuga1
Copy link
Contributor Author

samtuga1 commented Dec 1, 2023

I think your change is altering how jsonEncode works.

If toEncodable is omitted, it defaults to a function that returns the result of calling .toJson() on the unencodable object.

See here: https://api.flutter.dev/flutter/dart-convert/jsonEncode.html

So it means its either we make it session.toJson ourselves or check the type in the customEncoder and make it .toJson
I dont know why the toEncodable is changing the behaviour tho hmm...

@codekeyz
Copy link
Owner

codekeyz commented Dec 1, 2023

I think you should leave it as it. Any object being passed to res.json(Object) should either be serializable or have a .toJson() method.

If it fails, that's not our responsibility.

What we can do is add some comment to the res.json method to set expectations. @samtuga1

Let's get rid of the custom encoder.

@samtuga1
Copy link
Contributor Author

samtuga1 commented Dec 1, 2023

I think you should leave it as it. Any object being passed to res.json(Object) should either be serializable or have a .toJson() method.

If it fails, that's not our responsibility.

What we can do is add some comment to the res.json method to set expectations. @samtuga1

yeah so something like /// [params] must be encodable or? and give some examples (a value that is not a number, boolean, string, null, list or a map with string keys)

@samtuga1
Copy link
Contributor Author

samtuga1 commented Dec 1, 2023

I think you should leave it as it. Any object being passed to res.json(Object) should either be serializable or have a .toJson() method.
If it fails, that's not our responsibility.
What we can do is add some comment to the res.json method to set expectations. @samtuga1

Let's get rid of the custom encoder.

hmm...lets explore some other solutions to see

@samtuga1
Copy link
Contributor Author

samtuga1 commented Dec 1, 2023

I think you should leave it as it. Any object being passed to res.json(Object) should either be serializable or have a .toJson() method.
If it fails, that's not our responsibility.
What we can do is add some comment to the res.json method to set expectations. @samtuga1

Let's get rid of the custom encoder.

@codekeyz see here...this is dart's implementation of jsonEncode()

  String encode(Object? value, {Object? toEncodable(dynamic object)?}) {
    toEncodable ??= _toEncodable;
    if (toEncodable == null) return encoder.convert(value);

   // it tries to encode the non-encodable Objects below converting when the toEncodable != null
  // that is the problem
    return JsonEncoder(toEncodable).convert(value);
  }

  JsonEncoder get encoder {
    if (_toEncodable == null) return const JsonEncoder();
    return JsonEncoder(_toEncodable);
  }

maybe we can implement our own logic to work for us looking at this

edit: the .convert method uses a private class so not sure its possible

@codekeyz
Copy link
Owner

codekeyz commented Dec 1, 2023

I think you should leave it as it. Any object being passed to res.json(Object) should either be serializable or have a .toJson() method.
If it fails, that's not our responsibility.
What we can do is add some comment to the res.json method to set expectations. @samtuga1

Let's get rid of the custom encoder.

@codekeyz see here...this is dart's implementation of jsonEncode()

  String encode(Object? value, {Object? toEncodable(dynamic object)?}) {
    toEncodable ??= _toEncodable;
    if (toEncodable == null) return encoder.convert(value);

   // it tries to encode the non-encodable Objects below converting when the toEncodable != null
  // that is the problem
    return JsonEncoder(toEncodable).convert(value);
  }

  JsonEncoder get encoder {
    if (_toEncodable == null) return const JsonEncoder();
    return JsonEncoder(_toEncodable);
  }

maybe we can implement our own logic to work for us looking at this

Nope. Let's stay close to the language as much as possible. Let's just add the comments.

/// [object] should be json-encodable
Response json(Object? object);

If this comes up again as an issue, we'll look more into this. @samtuga1

@samtuga1
Copy link
Contributor Author

samtuga1 commented Dec 1, 2023

I think you should leave it as it. Any object being passed to res.json(Object) should either be serializable or have a .toJson() method.
If it fails, that's not our responsibility.
What we can do is add some comment to the res.json method to set expectations. @samtuga1

Let's get rid of the custom encoder.

@codekeyz see here...this is dart's implementation of jsonEncode()

  String encode(Object? value, {Object? toEncodable(dynamic object)?}) {
    toEncodable ??= _toEncodable;
    if (toEncodable == null) return encoder.convert(value);

   // it tries to encode the non-encodable Objects below converting when the toEncodable != null
  // that is the problem
    return JsonEncoder(toEncodable).convert(value);
  }

  JsonEncoder get encoder {
    if (_toEncodable == null) return const JsonEncoder();
    return JsonEncoder(_toEncodable);
  }

maybe we can implement our own logic to work for us looking at this

Nope. Let's stay close to the language as much as possible. Let's just add the comments.

/// [object] should be json-encodable
Response json(Object? object);

If this comes up again as an issue, we'll look more into this. @samtuga1

sure no problem, i'll add that and fix that in the test asap

@samtuga1 samtuga1 requested a review from codekeyz December 1, 2023 02:33
packages/pharaoh/lib/src/http/response.dart Outdated Show resolved Hide resolved
packages/pharaoh/test/middleware/session_mw_test.dart Outdated Show resolved Hide resolved
packages/pharaoh/lib/src/utils/utils.dart Outdated Show resolved Hide resolved
@samtuga1 samtuga1 requested a review from codekeyz December 1, 2023 09:31
@codekeyz codekeyz added good first issue Good for newcomers and removed bug Something isn't working labels Dec 1, 2023
@codekeyz codekeyz changed the title added a custom encoder and expose jwt package expose jwt package Dec 1, 2023
@codekeyz codekeyz merged commit 392b703 into codekeyz:main Dec 1, 2023
2 checks passed
samtuga1 added a commit to samtuga1/pharaoh that referenced this pull request Dec 2, 2023
@samtuga1 samtuga1 deleted the fix/datetime-encoder branch December 4, 2023 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fix: unable to encode DateTime in Response class
2 participants