GHMilestone throws NPE on null state
The following method is not null safe:
https://github.com/hub4j/github-api/blob/055638069ce8299629225be7f39395b16d4fe839/src/main/java/org/kohsuke/github/GHMilestone.java#L137-L138
Whenever it is called to get the state of the milestone, and it (state field) is null, it will throw a NullPointerException.
There is no other method in this class to ascertain the value of this before calling the method. The field is private. Programmers are forced to wrap this in a try/catch in the case it is null.
It would ease handling this call if it had a null check and returned null if there is no state.
PR welcome.
There's an enum helper class in this library that will supply a default value when enum conversion fails. It just wasn't used here.
@rnveach
When are you see state value of null?
The docs only mention open or closed: https://docs.github.com/en/rest/issues/milestones?apiVersion=2022-11-28#list-milestones
While scanning https://github.com/checkstyle/checkstyle/issues , I pick up issue 16243. I call GHIssue#listEvents and it eventually gives me a milestone issue event. So I call on that GHIssueEvent#getMilestone to get the milestone. I finally call on that GHMilestone#getState and that is where I run into the NPE issue.
https://api.github.com/repos/checkstyle/checkstyle/issues/16243/events
In my dump of GHMilestone instance in this case, everything is null except title.
Just my assumption not knowing anything and thinking about your question and seeing the events URL, maybe an event milestone (milestone in GHIssueEvent) has less information than standalone querying the actual milestone (GHMilestone)? Maybe you are mixing the 2 up here, and GHIssueEvent#getMilestone should return a smaller class than the full GHMilestone? I have, by no means, any expertise here.
If this is the case, then this issue isn't about null protection, but changing GHIssueEvent#getMilestone's return.
This seems to be the specific section in the REST API from the link you provided under response schema. https://docs.github.com/en/rest/issues/events?apiVersion=2022-11-28#list-issue-events
"milestone": {
"type": "object",
"properties": {
"title": {
"type": "string"
}
},
"required": [
"title"
]
}
Only title is expected back here.
I think this issue also expands to labels returned from GHIssueEvent.
Schema from spec:
"label": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"color": {
"type": "string"
}
},
"required": [
"name",
"color"
]
}
There is only name and color.
While scanning https://github.com/checkstyle/checkstyle/issues , I pick up issue 16243.
Ah, different endpoint. Got it.