Search code examples
orocommerce

How do I expose my extended field to the API?


I've started using OroCommerce, and wanted to try to extend a native OroCommerce entity to add a field to it.

For testing purposes, I've tried adding a "testField" field to the Product entity.

So I've created a migration class following the documentation, and that works fine:

class ExtendNativeMigration implements Migration
{
    public function up(Schema $schema, QueryBag $queries)
    {
        $table = $schema->getTable("oro_product");
        $table->addColumn('testField', "string", [
            "oro_options" => [
                "extend" => ["owner" => ExtendScope::OWNER_CUSTOM]
            ]
        ]);
    }
}

I now have a "testField" column in the oro_product table.

The problem I'm facing is that I can't seem to be able to expose that newly-created field to the API.

I created the api.yml file in my bundle, and tried something like this:

api:
  entities:
    Oro\Bundle\ProductBundle\Entity\Product:
      fields: 
        testField:
          exclude: false

But the field doesn't appear when I call the products API. I've tried other things in the api.yml file, but to no avail.

The field appears when I dump the API config, but it simply says "null":

...
    exclusion_policy: all
    fields:
        testField: null
        skuUppercase:
            exclude: true
...

I'm probably doing something wrong here, but I'm not sure what.

Can I have some pointers on how to do this correctly? Thank you!


Solution

  • Your code is absolutely fine. I've checked it locally, and the field is added to API with no issues.

    1. Make sure you have run php bin/console oro:api:cache:clear -eprod command after updating the API YAML configuration.

    2. Also, if you are working with the storefront API, you should use api_frontend.yml instead of api.yml file.