Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] APIv4: allow a field named "is_active" to default to false #27764

Conversation

semseysandor
Copy link
Contributor

Overview

If an entity has a field named "is_active" and it's default is defined as 0 in the schema, APIv4 overrides it to 1.
This effectively prevents these entities to be created as not active by default (when "is_active" is omitted from parameters).

Before

(Output trimmed for brevity)

$ cv api4 Dashboard.create
[
    {
        "id": 17,
        "is_active": true,
    }
]

After

(Output trimmed for brevity)

$ cv api4 Dashboard.create
[
    {
        "id": 18,
    }
]
$ cv api4 Dashboard.get +w 'id = 18'
[
    {
        "id": 18,
        "is_active": false,
    }
]

Technical Details

Similar to #27302

Comments

I ran into this in an extension, so it's possible to rename the field to "is_enabled" or something like that to workaround this limitation.
But there is one example in core: Dashboard entity, where APIv4 don't follow specifications, so this should be fixed.

@civibot
Copy link

civibot bot commented Oct 9, 2023

🤖 Thank you for contributing to CiviCRM! ❤️ We will need to test and review this PR. 👷

Introduction for new contributors...
  • If this is your first PR, an admin will greenlight automated testing with the command ok to test or add to whitelist.
  • A series of tests will automatically run. You can see the results at the bottom of this page (if there are any problems, it will include a link to see what went wrong).
  • A demo site will be built where anyone can try out a version of CiviCRM that includes your changes.
  • If this process needs to be repeated, an admin will issue the command test this please to rerun tests and build a new demo site.
  • Before this PR can be merged, it needs to be reviewed. Please keep in mind that reviewers are volunteers, and their response time can vary from a few hours to a few weeks depending on their availability and their knowledge of this particular part of CiviCRM.
  • A great way to speed up this process is to "trade reviews" with someone - find an open PR that you feel able to review, and leave a comment like "I'm reviewing this now, could you please review mine?" (include a link to yours). You don't have to wait for a response to get started (and you don't have to stop at one!) the more you review, the faster this process goes for everyone 😄
  • To ensure that you are credited properly in the final release notes, please add yourself to contributor-key.yml
  • For more information about contributing, see CONTRIBUTING.md.
Quick links for reviewers...

➡️ Online demo of this PR 🔗

@civibot civibot bot added the master label Oct 9, 2023
Comment on lines 100 to 102
if ($DAOField['name'] == 'is_active' && (!isset($DAOField['default']) || $DAOField['default'] === '')) {
$DAOField['default'] = '1';
}
Copy link
Member

Choose a reason for hiding this comment

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

Oof, I forgot this code existed. I think we should just delete it. Our schema used to be very inconsistent but there's been a lot of work lately to clean up boolean columns and ensure they always have the correct default set at the sql level so I think we can rely on that now and get rid of this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK. What about the test?
It seems a bit redundant after deletion, but more test is usually better than less 😃

Copy link
Member

Choose a reason for hiding this comment

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

Yes keep it. Always good to have a test!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed. I've squashed the commits for nicer history.

Copy link
Member

Choose a reason for hiding this comment

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

Perfect. Let's see what the test suite thinks of this.

Copy link
Contributor Author

@semseysandor semseysandor Oct 10, 2023

Choose a reason for hiding this comment

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

Looks like test suite doesn't like it...
I believe the reason is a missing <default>1</default> on civicrm_acl.is_active.
I've added a quick hack to simulate that and to verify tests pass with that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Quick hack didn't work so I did the proper way 😄
Missing default on civicrm_acl.is_active is just part of the reasons... Though there are 2 less failing test cases.

Seems, we need to go through the rest of the failing tests.
I think it's better to divide this PR into smaller parts and extract missing default on civicrm_acl.is_active as it's independent of this and needs to fixed anyway.

@semseysandor semseysandor changed the title APIv4: allow a field named "is_active" to default to false [WIP] APIv4: allow a field named "is_active" to default to false Oct 10, 2023
@semseysandor semseysandor marked this pull request as draft October 10, 2023 08:42
@colemanw
Copy link
Member

Reopened as #27846 and #27853

@colemanw colemanw closed this Oct 17, 2023
@semseysandor semseysandor deleted the is-active-default-0 branch October 19, 2023 06:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants