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

EnumFormatter.formatEnum throws a NoSuchElementException #190

Open
pbarron opened this issue Aug 11, 2018 · 2 comments
Open

EnumFormatter.formatEnum throws a NoSuchElementException #190

pbarron opened this issue Aug 11, 2018 · 2 comments

Comments

@pbarron
Copy link
Contributor

pbarron commented Aug 11, 2018

Noticed this while completing #181. When parsing an empty Json object the following exceptions is thrown

java.util.NoSuchElementException: None.get
	at scala.None$.get(Option.scala:349)
	at scala.None$.get(Option.scala:347)
	at skuber.json.format.package$EnumFormatter.$anonfun$formatEnum$3(package.scala:87)
	at scala.Option.getOrElse(Option.scala:121)
	at skuber.json.format.package$EnumFormatter.$anonfun$formatEnum$1(package.scala:87)
	at play.api.libs.json.JsSuccess.map(JsResult.scala:18)
	at play.api.libs.json.OFormat$$anon$5.$anonfun$inmap$1(Format.scala:29)
	at play.api.libs.json.OFormat$$anon$1.reads(Format.scala:37)
	at play.api.libs.json.Reads.$anonfun$map$1(Reads.scala:43)
	at play.api.libs.json.Reads$$anon$8.reads(Reads.scala:132)
	at play.api.libs.json.Reads$$anon$3$$anon$4.reads(Reads.scala:110)
	at play.api.libs.json.OFormat$$anon$2.reads(Format.scala:44)
...

instead of returning a JsError. The issue seems to be here where a get on the default optional value is made.

@doriordan
Copy link
Owner

Thanks @pbarronfor the bug report, so this looks like it will be thrown if no default is supplied by the applicable Json formatter but the field is missing, in this case no value can be returned.
At first glance I think the best solution might be to split formatEnum into two different functions - one (formatEnumWithDefault) has a mandatory default value parameter and the other (formatEnum) has no default parameter. formatNullable can be used with the first, knowing there is always a default to fall back on if it is missing, and the non-nullable variation offormat can be used with the second to automatically get a normal JsError if the value is missing.

@pbarron
Copy link
Contributor Author

pbarron commented Aug 14, 2018

so this looks like it will be thrown if no default is supplied by the applicable Json formatter but the field is missing, in this case no value can be returned.

Yes that is correct.

The solution you are suggesting should work. Though, probably also need to review the usages of the function where default is None to determine if the field needs to changed to an "Option" or left as is and return a JSError.

If i get time I'll have a look at it in the coming week.

cedarm pushed a commit to cedarm/skuber that referenced this issue Dec 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants