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

Failed to convert JsonString. #996

Closed
ghost opened this issue Dec 2, 2020 · 13 comments · Fixed by #1004
Closed

Failed to convert JsonString. #996

ghost opened this issue Dec 2, 2020 · 13 comments · Fixed by #1004
Labels

Comments

@ghost
Copy link

ghost commented Dec 2, 2020

How to convert an issue object to String? An error occurs during each conversion.

@bitwiseman
Copy link
Member

@lwx979458 What error? Please provide details

@ghost
Copy link
Author

ghost commented Dec 3, 2020

@bitwiseman
The error reported is:
com.fasterxml.jackson.databind.JsonMappingException: Null key for a Map not allowed in JSON (use a converting NullKeySerializer?) (through reference chain: org.kohsuke.github.GHIssue["responseHeaderFields"]->java.util.Collections$UnmodifiableMap["null"])

The obtained issue object is in the following format:
GHIssue@4b14c583[assignee=,assignees={},state=open,number=997,comments=0,labels=[],title=WARNING [com.squareup.okhttp.OkHttpClient] (OkHttp ConnectionPool) A connection to https://api.github.com/ was leaked. Did you forget to close a response body?,milestone=,locked=false,url=https://api.github.com/repos//issues/997,id=755961400,nodeId=MDU6SXNzdWU3NTU5NjE0MDA=,createdAt=2020-12-03T07:58:18Z,updatedAt=2020-12-03T07:58:18Z]

So I'll make an error when I convert it to JsonString. How can I get the normal form Object?

@bitwiseman
Copy link
Member

@lwx979458
Also please provide the actual stack trace from the "Null key for a Map not allowed in JSON" exception.

@ghost
Copy link
Author

ghost commented Dec 4, 2020

@bitwiseman
Sorry, I cannot find the corresponding Null key. I want to obtain full data of issues by page and convert the data to JsonString format. I do not know how to convert the data.

@bitwiseman
Copy link
Member

You said:

The error reported is:
com.fasterxml.jackson.databind.JsonMappingException: Null key for a Map not allowed in JSON (use a converting NullKeySerializer?) (through reference chain: org.kohsuke.github.GHIssue["responseHeaderFields"]->java.util.Collections$UnmodifiableMap["null"])

Please provide the rest of the stack trace from that exception.

@ghost
Copy link
Author

ghost commented Dec 9, 2020

@bitwiseman
The value of FinalresponseHeaders contains a null key.

@ghost
Copy link
Author

ghost commented Dec 9, 2020

@bitwiseman
PagedIterator<GHIssue> issuePagedIterator = gitHub.getRepository("hub4j/github-api").listIssues(GHIssueState.OPEN)._iterator(100);
I use the pagination function to obtain issue data.The value of FinalresponseHeaders contains a null key. As a result, I cannot convert Object to JsonString.

@ghost
Copy link
Author

ghost commented Dec 9, 2020

@bitwiseman
issue.getResponseHeaderFields()
{null=[HTTP/1.1 200 OK], Access-Control-Allow-Origin=[*], ...}
As described above, the first key returned is null.

@ghost ghost changed the title The toString method is abnormal. Failed to convert JsonString. Dec 14, 2020
@ghost
Copy link
Author

ghost commented Dec 15, 2020

How can I convert the obtained issue object to JsonString?

@bitwiseman
Copy link
Member

@lwx979458

Here's an example:

GHRepository repo = getTempRepository();
assertThat(repo.root, not(nullValue()));
String repoString = GitHub.getMappingObjectWriter().writeValueAsString(repo);
assertThat(repoString, not(nullValue()));

As a separate point, when someone asks for a stacktrace from an exception they mean something that looks like this:

java.io.FileNotFoundException: http://localhost:62277/app/installations/654321/access_tokens
	at org.kohsuke.github.extras.okhttp3.ObsoleteUrlFactory$OkHttpURLConnection.getInputStream(ObsoleteUrlFactory.java:488)
	at org.kohsuke.github.GitHubHttpUrlConnectionClient$HttpURLConnectionResponseInfo.bodyStream(GitHubHttpUrlConnectionClient.java:197)
	at org.kohsuke.github.GitHubResponse$ResponseInfo.getBodyAsString(GitHubResponse.java:326)
	at org.kohsuke.github.GitHubResponse.parseBody(GitHubResponse.java:91)
	at org.kohsuke.github.Requester.lambda$fetch$1(Requester.java:74)
	at org.kohsuke.github.GitHubClient.createResponse(GitHubClient.java:461)
	at org.kohsuke.github.GitHubClient.sendRequest(GitHubClient.java:412)
Caused: org.kohsuke.github.GHFileNotFoundException: http://localhost:62277/app/installations/654321/access_tokens {"message": "File not found"}
	at org.kohsuke.github.GitHubClient.interpretApiError(GitHubClient.java:492)
	at org.kohsuke.github.GitHubClient.sendRequest(GitHubClient.java:420)
	at org.kohsuke.github.GitHubClient.sendRequest(GitHubClient.java:363)
	at org.kohsuke.github.Requester.fetch(Requester.java:74)
... (more here clipped for clarity) ...

@ghost
Copy link
Author

ghost commented Dec 16, 2020

The org/kohsuke/github/GitHubResponse.java is modified to avoid the problem that the request header contains a null key.

TreeMap<String, List<String>> caseInsensitiveMap = new TreeMap<>(nullableCaseInsensitiveComparator);
for(String key: headers.keySet()){
    if(key != null){
        `caseInsensitiveMap.put(key, headers.get(key));
    }
}
this.headers = Collections.unmodifiableMap(caseInsensitiveMap);

@ghost
Copy link
Author

ghost commented Dec 16, 2020

GHIssue issue1 = gitHub.getRepository("hub4j/github-api").getIssue(997); 
System.out.println(JacksonJson.toJsonString(issue1));

com.fasterxml.jackson.databind.JsonMappingException: Null key for a Map not allowed in JSON (use a converting NullKeySerializer?) (through reference chain: org.kohsuke.github.GHIssue["responseHeaderFields"]->java.util.Collections$UnmodifiableMap["null"]) 
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:285) 
    at com.fasterxml.jackson.databind.SerializerProvider.mappingException(SerializerProvider.java:1251) 
    at com.fasterxml.jackson.databind.SerializerProvider.reportMappingProblem(SerializerProvider.java:1145) 
    at com.fasterxml.jackson.databind.ser.impl.FailingSerializer.serialize(FailingSerializer.java:35) 
    at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFields(MapSerializer.java:705) 
    at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:643) 
    at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:33) 
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727) 
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:722) 
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:166) 
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480) 
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319) 
    at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1433) 
    at com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:1135) 
    at com.fasterxml.jackson.databind.ObjectWriter.writeValueAsString(ObjectWriter.java:1005) 
    at org.gitlab4j.api.utils.JacksonJson.marshal(JacksonJson.java:247) 
    at org.gitlab4j.api.utils.JacksonJson.toJsonString(JacksonJson.java:367) 
    at org.huawei.com.Test1.main(Test1.java:45) 
JsonMappingException, message=Null key for a Map not allowed in JSON (use a converting NullKeySerializer?) (through reference chain: org.kohsuke.github.GHIssue["responseHeaderFields"]->java.util.Collections$UnmodifiableMap["null"]) 
null

I resolved the null key problem in the request header. However, when an object of an issue is converted to a JsonString, the request is continuously initiated.

{Access-Control-Allow-Origin=[*], Access-Control-Expose-Headers=[ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, Deprecation, Sunset], Cache-Control=[private, max-age=60, s-maxage=60], Connection=[Keep-Alive], Content-Security-Policy=[default-src 'none'], Content-Type=[application/json; charset=utf-8], Date=[Wed, 16 Dec 2020 03:00:10 GMT], ETag=[W/"71dc8958e466fd829f30f8e37854ea53717d068f334bb6ae86574859778d27bf"], Last-Modified=[Wed, 16 Dec 2020 02:33:43 GMT], Referrer-Policy=[origin-when-cross-origin, strict-origin-when-cross-origin], Server=[GitHub.com], Status=[200 OK], Strict-Transport-Security=[max-age=31536000; includeSubdomains; preload], Transfer-Encoding=[chunked], Vary=[Accept-Encoding, Accept, X-Requested-With, Accept, Authorization, Cookie, X-GitHub-OTP], via=[proxy A], X-Accepted-OAuth-Scopes=[repo], X-Content-Type-Options=[nosniff], X-Frame-Options=[deny], X-GitHub-Media-Type=[unknown, github.v3], X-GitHub-Request-Id=[3DEE:3ACF:465683:5966E2:5FD97839], X-OAuth-Scopes=[gist, read:org, repo], X-RateLimit-Limit=[5000], X-RateLimit-Remaining=[4996], X-RateLimit-Reset=[1608091208], X-RateLimit-Used=[4], X-XSS-Protection=[1; mode=block]}

{Access-Control-Allow-Origin=[*], Access-Control-Expose-Headers=[ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, Deprecation, Sunset], Cache-Control=[private, max-age=60, s-maxage=60], Connection=[Keep-Alive], Content-Security-Policy=[default-src 'none'], Content-Type=[application/json; charset=utf-8], Date=[Wed, 16 Dec 2020 03:00:11 GMT], ETag=[W/"01d1a44c2c6b64a09d9abef7f49f6fac686a79f8c4f0c3d61b018ec9c5d4172d"], Referrer-Policy=[origin-when-cross-origin, strict-origin-when-cross-origin], Server=[GitHub.com], Status=[200 OK], Strict-Transport-Security=[max-age=31536000; includeSubdomains; preload], Transfer-Encoding=[chunked], Vary=[Accept-Encoding, Accept, X-Requested-With, Accept, Authorization, Cookie, X-GitHub-OTP], via=[proxy A], X-Accepted-OAuth-Scopes=[], X-Content-Type-Options=[nosniff], X-Frame-Options=[deny], X-GitHub-Media-Type=[unknown, github.v3], X-GitHub-Request-Id=[3DEE:3ACF:4656C5:59671E:5FD9783A], X-OAuth-Scopes=[gist, read:org, repo], X-RateLimit-Limit=[5000], X-RateLimit-Remaining=[4995], X-RateLimit-Reset=[1608091208], X-RateLimit-Used=[5], X-XSS-Protection=[1; mode=block]}

{Access-Control-Allow-Origin=[*], Access-Control-Expose-Headers=[ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, Deprecation, Sunset], Cache-Control=[private, max-age=60, s-maxage=60], Connection=[Keep-Alive], Content-Security-Policy=[default-src 'none'], Content-Type=[application/json; charset=utf-8], Date=[Wed, 16 Dec 2020 03:00:11 GMT], ETag=[W/"388d579dbffb018a1b6de0c51e48703cfcb475beffc92952a335f6ed5ef11386"], Last-Modified=[Tue, 24 Nov 2020 19:23:21 GMT], Referrer-Policy=[origin-when-cross-origin, strict-origin-when-cross-origin], Server=[GitHub.com], Status=[200 OK], Strict-Transport-Security=[max-age=31536000; includeSubdomains; preload], Transfer-Encoding=[chunked], Vary=[Accept-Encoding, Accept, X-Requested-With, Accept, Authorization, Cookie, X-GitHub-OTP], via=[proxy A], X-Accepted-OAuth-Scopes=[], X-Content-Type-Options=[nosniff], X-Frame-Options=[deny], X-GitHub-Media-Type=[unknown, github.v3], X-GitHub-Request-Id=[3DEE:3ACF:4656EA:59675B:5FD9783B], X-OAuth-Scopes=[gist, read:org, repo], X-RateLimit-Limit=[5000], X-RateLimit-Remaining=[4994], X-RateLimit-Reset=[1608091208], X-RateLimit-Used=[6], X-XSS-Protection=[1; mode=block]}

{Access-Control-Allow-Origin=[*], Access-Control-Expose-Headers=[ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, Deprecation, Sunset], Cache-Control=[private, max-age=60, s-maxage=60], Connection=[Keep-Alive], Content-Security-Policy=[default-src 'none'], Content-Type=[application/json; charset=utf-8], Date=[Wed, 16 Dec 2020 03:00:12 GMT], ETag=[W/"f1236c3c63f7bb598f3c3f9cdfeb27f7242625d6ff1dfdc00cd668350f13b60a"], Link=[<https://api.github.com/user/1958953/followers?page=2>; rel="next", <https://api.github.com/user/1958953/followers?page=6>; rel="last"], Referrer-Policy=[origin-when-cross-origin, strict-origin-when-cross-origin], Server=[GitHub.com], Status=[200 OK], Strict-Transport-Security=[max-age=31536000; includeSubdomains; preload], Transfer-Encoding=[chunked], Vary=[Accept-Encoding, Accept, X-Requested-With, Accept, Authorization, Cookie, X-GitHub-OTP], via=[proxy A], X-Accepted-OAuth-Scopes=[], X-Content-Type-Options=[nosniff], X-Frame-Options=[deny], X-GitHub-Media-Type=[unknown, github.v3], X-GitHub-Request-Id=[3DEE:3ACF:465703:596779:5FD9783B], X-OAuth-Scopes=[gist, read:org, repo], X-RateLimit-Limit=[5000], X-RateLimit-Remaining=[4993], X-RateLimit-Reset=[1608091208], X-RateLimit-Used=[7], X-XSS-Protection=[1; mode=block]}

{Access-Control-Allow-Origin=[*], Access-Control-Expose-Headers=[ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, Deprecation, Sunset], Cache-Control=[private, max-age=60, s-maxage=60], Connection=[Keep-Alive], Content-Security-Policy=[default-src 'none'], Content-Type=[application/json; charset=utf-8], Date=[Wed, 16 Dec 2020 03:00:12 GMT], ETag=[W/"05cf42714b3f09c7017bd735bfda5ea6420cb88526a8678ecf74d87501ca36ed"], Link=[<https://api.github.com/user/1958953/followers?page=1>; rel="prev", <https://api.github.com/user/1958953/followers?page=3>; rel="next", <https://api.github.com/user/1958953/followers?page=6>; rel="last", <https://api.github.com/user/1958953/followers?page=1>; rel="first"], Referrer-Policy=[origin-when-cross-origin, strict-origin-when-cross-origin], Server=[GitHub.com], Status=[200 OK], Strict-Transport-Security=[max-age=31536000; includeSubdomains; preload], Transfer-Encoding=[chunked], Vary=[Accept-Encoding, Accept, X-Requested-With, Accept, Authorization, Cookie, X-GitHub-OTP], via=[proxy A], X-Accepted-OAuth-Scopes=[], X-Content-Type-Options=[nosniff], X-Frame-Options=[deny], X-GitHub-Media-Type=[unknown, github.v3], X-GitHub-Request-Id=[3DEE:3ACF:465727:5967A4:5FD9783C], X-OAuth-Scopes=[gist, read:org, repo], X-RateLimit-Limit=[5000], X-RateLimit-Remaining=[4992], X-RateLimit-Reset=[1608091208], X-RateLimit-Used=[8], X-XSS-Protection=[1; mode=block]}

...

@bitwiseman
Copy link
Member

bitwiseman commented Dec 17, 2020

@lwx979458 (I updated your comment to use code blocks (triple-backtick).

Okay, this is bug. I see the problem. Certain fields that should be transient are not. I'll get a PR up for this when I have the chance. In the meanwhile, you can manually exclude root and responseHeaderFields (and any other problem field) using one of the techniques here: https://stackoverflow.com/questions/13764280/how-do-i-exclude-fields-with-jackson-not-using-annotations .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant