Skip to content

Conversation

@n1k0
Copy link
Member

@n1k0 n1k0 commented Oct 7, 2024

🔧 Problem

Nullable fields weren't nullable anymore when parsing JSON queries in the Food API. See card.

🍰 Solution

This patch uses a more flexible utilitity from Decode.Extra to relax nullable value check. It also ensures always responding with errors encoded as JSON, for consistency.

🏝️ How to test

Unit tests have been added so we're probably safe here, but toying around with the API manually might be a good idea.

Production

$ curl 'https://ecobalyse.beta.gouv.fr/api/food' \
  -H "accept: application/json" \
  -H "content-type: application/json" \
  -d '{"ingredients": [{"id": "milk","mass": 500,"country": "BR"}],"transform": null,"packaging": [],"distribution": "ambient","preparation":[]}'
Problem with the value at json.transform:

    null

Expecting an OBJECT with a field named `code`

Patch version

A null transform is now accepted:

$ curl 'http://localhost:1234/api/food' \
  -H "accept: application/json" \
  -H "content-type: application/json" \
  -d '{"ingredients": [{"id": "milk","mass": 500,"country": "BR"}],"transform": null,"packaging": [],"distribution": "ambient","preparation":[]}'
{"webUrl":"https://ecobalyse.beta.gouv.fr/#/food/ecs/eyJpbmdyZWRpZW50cyI6W3siaWQiOiJtaWxrIiwibWFzcyI6NTAwLCJjb3VudHJ5IjoiQlIifV0sImRpc3RyaWJ1dGlvbiI6ImFtYmllbnQifQ==","results":{"total":{"acd":0,"cch":0,"etf":0,"etf-c":0,"fru":0,"fwe":0,"htc":0,"htc-c":0,"htn":0,"htn-c":0,"ior":0,"ldu":0,"mru":0,"ozd":0,"pco":0,"pma":0,"swe":0,"tre":0,"wtu":0,"ecs":88.24043055316605,"pef":78.16321668566478},"perKg":{"acd":0,"cch":0,"etf":0,"etf-c":0,"fru":0,"fwe":0,"htc":0,"htc-c":0,"htn":0,"htn-c":0,"ior":0,"ldu":0,"mru":0,"ozd":0,"pco":0,"pma":0,"swe":0,"tre":0,"wtu":0,"ecs":176.4808611063321,"pef":156.32643337132956},"scoring":{"all":176.4808611063321,"climate":0,"biodiversity":0,"health":0,"resources":0},"totalMass":0.5,"preparedMass":0.5,"recipe":{"total":{"acd":0,"cch":0,"etf":0,"etf-c":0,"fru":0,"fwe":0,"htc":0,"htc-c":0,"htn":0,"htn-c":0,"ior":0,"ldu":0,"mru":0,"ozd":0,"pco":0,"pma":0,"swe":0,"tre":0,"wtu":0,"ecs":79.83329037441166,"pef":69.32826980137915},"ingredientsTotal":{"acd":0,"cch":0,"etf":0,"etf-c":0,"fru":0,"fwe":0,"htc":0,"htc-c":0,"htn":0,"htn-c":0,"ior":0,"ldu":0,"mru":0,"ozd":0,"pco":0,"pma":0,"swe":0,"tre":0,"wtu":0,"ecs":62.11529284239579,"pef":50.43721172589145},"totalBonusImpact":{"cropDiversity":0,"hedges":-1.005370395,"livestockDensity":0.255,"microfibers":0,"outOfEuropeEOL":0,"permanentPasture":-0.6124999999999999,"plotSize":-1.26399895},"transform":{"acd":0,"cch":0,"etf":0,"etf-c":0,"fru":0,"fwe":0,"htc":0,"htc-c":0,"htn":0,"htn-c":0,"ior":0,"ldu":0,"mru":0,"ozd":0,"pco":0,"pma":0,"swe":0,"tre":0,"wtu":0,"ecs":0,"pef":0},"transports":{"air":0,"impacts":{"acd":0,"cch":0,"etf":0,"etf-c":0,"fru":0,"fwe":0,"htc":0,"htc-c":0,"htn":0,"htn-c":0,"ior":0,"ldu":0,"mru":0,"ozd":0,"pco":0,"pma":0,"swe":0,"tre":0,"wtu":0,"ecs":17.717997532015875,"pef":18.891058075487692},"road":0,"roadCooled":660,"sea":0,"seaCooled":8315}},"packaging":{"acd":0,"cch":0,"etf":0,"etf-c":0,"fru":0,"fwe":0,"htc":0,"htc-c":0,"htn":0,"htn-c":0,"ior":0,"ldu":0,"mru":0,"ozd":0,"pco":0,"pma":0,"swe":0,"tre":0,"wtu":0,"ecs":0,"pef":0},"preparation":{"acd":0,"cch":0,"etf":0,"etf-c":0,"fru":0,"fwe":0,"htc":0,"htc-c":0,"htn":0,"htn-c":0,"ior":0,"ldu":0,"mru":0,"ozd":0,"pco":0,"pma":0,"swe":0,"tre":0,"wtu":0,"ecs":0,"pef":0},"transports":{"air":0,"impacts":{"acd":0,"cch":0,"etf":0,"etf-c":0,"fru":0,"fwe":0,"htc":0,"htc-c":0,"htn":0,"htn-c":0,"ior":0,"ldu":0,"mru":0,"ozd":0,"pco":0,"pma":0,"swe":0,"tre":0,"wtu":0,"ecs":24.456621136766252,"pef":25.682404001519505},"road":0,"roadCooled":1260,"sea":0,"seaCooled":8315},"distribution":{"total":{"acd":0,"cch":0,"etf":0,"etf-c":0,"fru":0,"fwe":0,"htc":0,"htc-c":0,"htn":0,"htn-c":0,"ior":0,"ldu":0,"mru":0,"ozd":0,"pco":0,"pma":0,"swe":0,"tre":0,"wtu":0,"ecs":1.6685165740040186,"pef":2.043600958253823},"transports":{"air":0,"impacts":{"acd":0,"cch":0,"etf":0,"etf-c":0,"fru":0,"fwe":0,"htc":0,"htc-c":0,"htn":0,"htn-c":0,"ior":0,"ldu":0,"mru":0,"ozd":0,"pco":0,"pma":0,"swe":0,"tre":0,"wtu":0,"ecs":6.738623604750378,"pef":6.791345926031812},"road":0,"roadCooled":600,"sea":0,"seaCooled":0}}},"description":"TODO","query":{"ingredients":[{"id":"milk","mass":500,"country":"BR"}],"distribution":"ambient"}}

Decoding errors are now wrapped in JSON:

$ curl 'http://localhost:1234/api/food' \
  -H "accept: application/json" \
  -d '{"ingredients": [{"id": "milk","mass": 500,"country": "BR"}],"transform": null,"packaging": [],"distribution": "ambient","preparation":[]}'
{"error":["Problem with the given value:","","{}","","Expecting an OBJECT with a field named `ingredients`"],"documentation":"https://ecobalyse.beta.gouv.fr/#/api"}

@n1k0 n1k0 requested a review from vjousse October 7, 2024 15:14
@n1k0 n1k0 force-pushed the fix/food-api-json-query-parser branch from 93c1e21 to 88704aa Compare October 7, 2024 15:15
@n1k0 n1k0 force-pushed the fix/food-api-json-query-parser branch from 88704aa to 1adc1e5 Compare October 7, 2024 15:29
@n1k0 n1k0 force-pushed the fix/food-api-json-query-parser branch from f5049cb to f54b40c Compare October 7, 2024 16:03
Copy link
Collaborator

@vjousse vjousse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally, works as expected 👍 Are we ok with changing the api URL? Do we need to do some special communication?

@n1k0
Copy link
Member Author

n1k0 commented Oct 8, 2024

Are we ok with changing the api URL? Do we need to do some special communication?

The URL was erroneous ;)

@n1k0 n1k0 merged commit 3bcc269 into master Oct 8, 2024
@n1k0 n1k0 deleted the fix/food-api-json-query-parser branch October 8, 2024 09:16
vjousse pushed a commit that referenced this pull request Oct 10, 2024
## [2.4.0](https://github.com/MTES-MCT/ecobalyse/compare/v2.3.0..v2.4.0)
(2024-10-10)



### 🚀 Features

- Introduce first version of object interface
([#756](#756))

### 🪲 Bug Fixes

- Sync food ([#759](#759))
- Don't hide version information on staging
([#778](#778))
- Reset physical durablility in regulatory mode
([#786](#786))
- *(api,food)* Nullable fields weren't nullable anymore.
([#789](#789))

### 🚜 Refactor

- Small textile explorer improvements
([#773](#773))

### ⚙️ Miscellaneous Tasks

- Don't download draft releases
([#771](#771))
- Remove `airTransportRatio` from examples
([#785](#785))
- Cleanup package-lock.json.
([#787](#787))
- Use builtin python action cache for pipenv
([#796](#796))
- Improve changelog by using `git-cliff`
([#768](#768))

<!-- generated by git-cliff -->

Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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

Successfully merging this pull request may close these issues.

3 participants