Thalhammer / jwt-cpp

A header only library for creating and validating json web tokens in c++
https://thalhammer.github.io/jwt-cpp/
MIT License
856 stars 233 forks source link

error: ‘class jwt::decoded_jwt<jwt::traits::nlohmann_json>’ has no member named ‘get_payload_json’; #286

Closed kiner-shah closed 1 year ago

kiner-shah commented 1 year ago

What happened?

Tried to follow the example in Getting Started section of README with slight modifications. It threw a compiler error stating a method was not found which is weird.

I think the issue can be fixed by using decoded_jwt::get_payload_claims() function. The README code may need to be changed.

I also noticed that this issue in README is there in the master branch. When I check README of v0.6.0 release artifacts, I see get_payload_claims() mentioned correctly there.

How To Reproduce?

#include "jwt-cpp/traits/nlohmann-json/traits.h"
#include <iostream>

int main()
{
    std::string token = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.VFb0qJ1LRg_4ujbZoRMXnVkUgiuKq5KxWqNdbKq_G9Vvz-S1zZa9LPxtHWKa64zDl2ofkT8F6jBt_K4riU-fPg";
    auto decoded = jwt::decode<jwt::traits::nlohmann_json>(token);
    for (auto& [k, v] : decoded.get_payload_json())
        std::cout << k << '=' << v << '\n';
}

Run with: g++ main.cpp -I /path/to/jwt-cpp-0.6.0/include/

Version

0.6.0

What OS are you seeing the problem on?

Linux

What compiler are you seeing the problem on?

GCC

Relevant log output

main.cpp: In function ‘int main()’:
main.cpp:8:33: error: ‘class jwt::decoded_jwt<jwt::traits::nlohmann_json>’ has no member named ‘get_payload_json’; did you mean ‘get_payload’?
    8 |     for (auto& [k, v] : decoded.get_payload_json())
      |                                 ^~~~~~~~~~~~~~~~
      |                                 get_payload


### Code of Conduct

- [X] I agree to follow this project's Code of Conduct
prince-chrismc commented 1 year ago

I am not sure what the issue is 🤔

0.6.0 matches the readme and master matches? The code in question is unreleased https://github.com/Thalhammer/jwt-cpp/commit/cba92ab173f45bb028f287b4acdbdb9d06352499 is that what might have tripped you up?

The releases are infrequent since it's very stable but I dont think it's an issue for master docs to match the master code. Thats very common.

kiner-shah commented 1 year ago

@prince-chrismc Yes it seems I got confused because I downloaded the release artifacts but kept the repo's URL open in the browser which showed the master branch README which had a new API in the example.

I see you mentioned the exact commit (cba92ab) where this API was changed. I think it would be better to keep the older API but with some deprecation warning in the new release. Else, it can completely break some code that used the old API and now wants to migrate to a new version due to some reasons.

Thalhammer commented 1 year ago

I think it would be better to keep the older API but with some deprecation warning in the new release.

I generally agree, but jwt-cpp had way more impactful API breaks in the past. In this case for most codebases it will boil down to replacing all occurrences of get_payload_claims with get_payload_json. We'd like to adopt semver eventually, but I'd like to make sure its actually usable before, so we don't end up with a version 200.1.1.

prince-chrismc commented 1 year ago

We do try to avoid breaking the API, there are most often aliases left to avoid any impact. It's a hardline to balance improvements and not changing.

Ticked this as closed since the docs for that release were correct :)