Problem/Motivation
It would be convenient if the decimal representation of a fraction field could be included in JSONAPI responses. Such a change may also include the decimal value in other places that the normalized fraction is used, depending on how the fraction is normalized.
The Drupal core JSONAPI module FieldItemNormalizer only includes not-internal properties: https://git.drupalcode.org/project/drupal/-/blob/9.3.x/core/modules/json...
Unless specified, all computed data definitions are "internal" by default: https://git.drupalcode.org/project/drupal/-/blob/9.3.x/core/lib/Drupal/C...
Steps to reproduce
N/A
Proposed resolution
Set the fraction value property definition to not be internal.
Remaining tasks
Implement
User interface changes
None.
API changes
The fraction decimal value is included in JSONAPI.
Data model changes
Issue fork fraction-3243485
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
paul121 commentedAn example of the JSONAPI response for a farmOS quantity entity with a fraction field named "value":
This kinda makes we wonder if this property should be named
decimalinstead. I think that might make more sense to the JSONAPI consumer that is unfamiliar with Drupal, but make less sense in the context of Drupal entity API and common conventions.Comment #4
paul121 commentedI think these titles change when the issue fork branch is changed from the default name?
Comment #5
m.stentaAwesome! Thanks @paul121! I'm amazed at how simple this change is. :-)
I think we should add some tests for this, though - even though it's so simple. It feels like the type of thing that could accidentally regress in the future if someone removes that line without understanding why it's there.
The same thought came to my mind too. I'm open to that instead of
valueif it makes more sense.It actually might be *good* to avoid
valueso that folks who *are* used to Drupal conventions are forced to understand that this is *not* a normalvaluefield.If we do change to
decimal, does that affect #3243488: Allow fraction field item value to be set from a decimal value at all?Comment #6
paul121 commentedOhh interesting. I was starting go back on this, but this is a good point. +1
Not that I can think of, other than changing some strings from
valuetodecimal. Shall we give it a go?That's fine. It should be simple to add a test ensuring the field is not internal. Considering that we want tests in this other issue, that would actually be a good reason to start a new kernel test. Might as well start simple and merge this, then we can tackle the other issue. I'll work on tests tomorrow.
Comment #7
paul121 commentedI made this change but noticed that the fraction formatter was actually using the
valueproperty itself. I changed the formatter to usedecimalinstead, but it just feels like this could be a bit of a BC.I tacked on one last commit that adds back the
valueproperty but keeps it internal so that it is still excluded from the API. Maybe we could make thevalueproperty trigger a deprecation warning by creating a dedicated FarmDecimalPropertyDeprecated class? Not sure how concerned we are about this. It doesn't look like farmOS 2.x uses thevalueproperty right now, but I know we aren't the only users of Fraction!This was actually pretty easy! I was able to re-use an existing kernel test class - generalized it from testing only "generate sample values" to testing the FractionItem more generally. Added some simple assertions in a new
testFieldProperties()function.On a new note... I'm a little curious why the fraction value/decimal property always uses a precision of 9? https://git.drupalcode.org/project/fraction/-/blob/2.x/src/FractionDecim...
Any reason this can't just be auto? I might be missing something! I see the comment about ensuring validation, but don't understand. Maybe the validation function could explicitly convert to decimal with a precision of 9? More context in this comment from @pcambra: https://www.drupal.org/project/fraction/issues/3095746#comment-13450953
Comment #8
m.stentaThanks @paul121! This is a nice improvement. :-)
I'm going to leave this commit out. You're right it's a breaking change, but I think the risk is low. I *just* released 2.0.0 yesterday, and made it clear in the release notes that there are breaking changes. Technically we shouldn't include breaking changes moving forward, but in the interest of cleanliness/simplicity, and given that 2.0.0 is so new, and the risk is low, I'm going to make an exception and tag a new release with this in it today or tomorrow. I'll mention this in the release notes, just in case. This may come back to bite me (hopefully not) - but if it does we can add this back in.
Huh. Yea I noticed that in your JSON above. That shouldn't be. I'll have a closer look and maybe open a new issue...
Comment #10
m.stentaComment #11
m.stenta#3252245: Use automatic precision for decimal field property
Comment #12
m.stentaOops I should have tested this first. Tests are failing with
InvalidArgumentException: Property value is unknown. in Drupal\Core\TypedData\TypedDataManager->getPropertyInstance():-/Comment #13
m.stentaAh - this seems to be some kind of deeper assumption related to
FractionConstraint/FractionConstraintValidator. I can replicate it locally only when a min/max is defined for the field (which is also what was being tested in one of the tests that's failing).Comment #14
m.stentaOpened a separate issue with a merge request to fix this: #3252305: Range constraint assumes value property exists
Comment #15
paul121 commentedWoohoo, thanks @m.stenta! It seems that issue is fixed now? Can we close this issue?
Comment #16
m.stentaOops! Yes - meant to close it when I opened the new one. :-)