FriendsOfCake / crud-json-api

Build advanced JSON API Servers with almost no code.
https://crud-json-api.readthedocs.io/
MIT License
55 stars 29 forks source link

Not returning Table Column names with "_" when using fields[model] #89

Closed joakim-emagine closed 3 years ago

joakim-emagine commented 5 years ago

For some reason the Api completely ignores columns with "_" in their names when specified with the fields[model]=column_name.

Without 'fields' option:

{
    "data": {
        "type": "countries",
        "id": "1",
        "attributes": {
            "ctype": "Independent State",
            "sovereignty": "",
            "capital": "Kabul",
            "common-name": "Afghanistan",
            "group-name": "Rest of the World",
            "formal-name": "Islamic State of Afghanistan",
            "sub-type": "",
            "i-s-o-4217-currency-code": "AFN",
            "i-s-o-4217-currency-name": "Afghani",
            "i-t-u-t-telephone-code": "+93",
            "i-s-o-31661-2-letter-code": "AF",
            "i-s-o-31661-3-letter-code": "AFG",
            "i-s-o-31661-number": "004",
            "i-a-n-a-country-code-t-l-d": ".af"
        },
        "links": {
            "self": "/api/countries/1"
        }
    },
    "query": {
        "default": [
            {
                "query": "SELECT Countries.id AS `Countries__id`, Countries.common_name AS `Countries__common_name`, Countries.group_name AS `Countries__group_name`, Countries.formal_name AS `Countries__formal_name`, Countries.ctype AS `Countries__ctype`, Countries.sub_type AS `Countries__sub_type`, Countries.sovereignty AS `Countries__sovereignty`, Countries.capital AS `Countries__capital`, Countries.ISO_4217_currency_code AS `Countries__ISO_4217_currency_code`, Countries.ISO_4217_currency_name AS `Countries__ISO_4217_currency_name`, Countries.ITU_T_telephone_code AS `Countries__ITU_T_telephone_code`, Countries.ISO_31661_2_letter_code AS `Countries__ISO_31661_2_letter_code`, Countries.ISO_31661_3_letter_code AS `Countries__ISO_31661_3_letter_code`, Countries.ISO_31661_number AS `Countries__ISO_31661_number`, Countries.IANA_country_code_TLD AS `Countries__IANA_country_code_TLD` FROM countries Countries WHERE Countries.id = 1 LIMIT 1",
                "took": 1,
                "params": {
                    "c0": 1
                },
                "numRows": 1,
                "error": null
            }
        ],
        "test": [],
        "debug_kit": []
    }
}

With ?fields[countries]=id,ctype,common_name,capital:

{
    "data": {
        "type": "countries",
        "id": "1",
        "attributes": {
            "ctype": "Independent State",
            "capital": "Kabul"
        },
        "links": {
            "self": "/api/countries/1"
        }
    }
},
    "query": {
        "default": [
            {
                "query": "SELECT Countries.id AS `Countries__id`, Countries.ctype AS `Countries__ctype`, Countries.common_name AS `Countries__common_name`, Countries.capital AS `Countries__capital` FROM countries Countries WHERE Countries.id = 1 LIMIT 1",
                "took": 1,
                "params": {
                    "c0": 1
                },
                "numRows": 1,
                "error": null
            }
        ],
        "test": [],
        "debug_kit": []
    }
}
bravo-kernel commented 5 years ago

Can you try using dasherized, it should translate to the underscored column name internally.

joakim-emagine commented 5 years ago

Hi, I am already using this setup in routes.php

Router::prefix('api', function ($routes) {
   // $routes->extensions(['json', 'xml']);
    $routes->resources('Countries', [
          'inflect' => 'dasherize'
          ]
    );
});
bravo-kernel commented 5 years ago

I meant calling the url with the dasherized column name. E.g.

fields[countries]=id,ctype,common-name

joakim-emagine commented 5 years ago

I meant calling the url with the dasherized column name. E.g.

fields[countries]=id,ctype,common-name

?fields[countries]=id,ctype,common-name,capital

ok, see result here:

{
"data": {
"type": "countries",
"id": "1",
"attributes": {
"ctype": "Independent State",
"capital": "Kabul"
},
"links": {
"self": "/api/countries/1"
}
},
"query": {
"default": [
{
"query": "SELECT Countries.id AS `Countries__id`, Countries.ctype AS `Countries__ctype`, Countries.capital AS `Countries__capital` FROM countries Countries WHERE Countries.id = 1 LIMIT 1",
"took": 1,
"params": {
"c0": 1
},
"numRows": 1,
"error": null
}
],
"test": [],
"debug_kit": []
}
}
bravo-kernel commented 5 years ago

I just checked and there is no test case for Sparse Fieldsets using multi-word fields yet so you could be right. Would you like to assist in fixing this problem? A good place to start would be adding a failing test-case.

https://github.com/FriendsOfCake/crud-json-api/blob/master/tests/TestCase/Integration/JsonApi/SparseFieldsetsIntegrationTest.php

joakim-emagine commented 5 years ago

Okay, will see what I can do.

geoidesic commented 4 years ago

Any progress on this?

dakota commented 3 years ago

Closing as stale. Please reopen if this is still an issue.