From 73d89e435c7852f41aaa4c3bd45966c8674e7d5c Mon Sep 17 00:00:00 2001 From: Kelvin Date: Thu, 29 Oct 2026 20:13:11 +0530 Subject: [PATCH] feat: require marketCode in mandi-price select [OpenAgriNet/network-adapter#97] Refuse a select whose market.marketCode is absent or empty before Agmarknet is called; without it the query widens to the whole district. Each on_select resource now carries market.marketCode, in the same place catalog-ondemand.yaml puts it. --- .../agmarknet/mandi-price.select.yaml | 22 ++++++++-------- .../MandiPrice/mappings_test.go | 25 ++++++++++++++++--- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/config/mappings/agmarknet/mandi-price.select.yaml b/config/mappings/agmarknet/mandi-price.select.yaml index 841618389..a62ec673f 100644 --- a/config/mappings/agmarknet/mandi-price.select.yaml +++ b/config/mappings/agmarknet/mandi-price.select.yaml @@ -80,6 +80,14 @@ required: and $match($ra.market.state, /^[A-Za-z]{2,4}$/) ) message: "this capability needs Agmarknet's codes in market: a numeric district and a short alphabetic state, not their names" + - check: | + ( + $ra := beckn.message.contract.commitments[0].resources[0].resourceAttributes; + /* market.marketCode, where the catalog advertises it. Absent or empty, Agmarknet + widens the query to the whole district. $string("") is not "" here, so compare raw. */ + $exists($ra.market.marketCode) and $ra.market.marketCode != "" + ) + message: "this capability needs a market code in market.marketCode, as the catalog lists it; without it the query widens to the whole district" - check: | ( $ra := beckn.message.contract.commitments[0].resources[0].resourceAttributes; @@ -338,18 +346,10 @@ response: | "commodityGroup": $r.Group, "variety": $r.Variety, "grade": $r.Grade, - /* Every member comes from the RECORD, so the answer describes what - the provider reported rather than what was asked for. - - marketCode is absent, and that is not an omission. This upstream - TAKES a market code as a query parameter and reports none back -- - there is no such field anywhere in its response. Restating the - requested code against a returned row would assert something - unverified, and district-wide there is no requested code at all - while the rows come from several markets, so one code would have - been wrong for most of them. The pack requires only marketName - and calls marketCode "when available"; here it is not. */ + /* From the RECORD, except marketCode: the upstream reports none, so it is the + requested one (guarded), placed where catalog-ondemand.yaml puts it. */ "market": { + "marketCode": $ra.market.marketCode, "marketName": $r.Market, "district": $r.District, "state": $r.State diff --git a/pkg/plugin/implementation/MandiPrice/mappings_test.go b/pkg/plugin/implementation/MandiPrice/mappings_test.go index dc902b48a..afd2683f3 100644 --- a/pkg/plugin/implementation/MandiPrice/mappings_test.go +++ b/pkg/plugin/implementation/MandiPrice/mappings_test.go @@ -683,9 +683,11 @@ func TestShippedMappingStatesWhatItKnowsRatherThanEchoing(t *testing.T) { } market, _ := ra["market"].(map[string]any) - // The upstream reports no market code, so the answer must not claim one. - if code, present := market["marketCode"]; present { - t.Errorf("market.marketCode = %#v; this upstream reports no code, so asserting one is unfounded", code) + // The upstream reports no market code, but the select requires one and + // the query is scoped to it, so every row is that market's. The code + // sits where the catalog advertised it: market.marketCode. + if market["marketCode"] != "2056" { + t.Errorf("market.marketCode = %#v, want the requested \"2056\"", market["marketCode"]) } // And what is there comes from the record. if market["marketName"] != "Kasdol APMC" || market["state"] != "Chattisgarh" { @@ -722,6 +724,23 @@ func TestShippedMappingRefusesPayloadsItCannotAnswer(t *testing.T) { }, expect: "codes in market", }, + { + // Without a market code the upstream widens the query to the whole + // district, and the answer can no longer say which market it is for. + name: "no market code", + mutate: func(ra map[string]any) { + delete(ra["market"].(map[string]any), "marketCode") + }, + expect: "market code", + }, + { + // Present but empty names no market either. + name: "an empty market code", + mutate: func(ra map[string]any) { + ra["market"].(map[string]any)["marketCode"] = "" + }, + expect: "market code", + }, { // Everything downstream reads supportedCommodities[0], so this // used to be queried for Paddy alone and answered confidently.