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

fix: get schemata defined in the context #1796

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/1796.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Get schemata for behaviors defined in the context. @mamico
2 changes: 2 additions & 0 deletions src/plone/restapi/testing.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@
provides=".tests.dxtypes.ITestAnnotationsBehavior"
/>

<adapter factory=".tests.dxtypes.DexterityInstanceBehaviorAssignable" />

</configure>
23 changes: 23 additions & 0 deletions src/plone/restapi/tests/dxtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
from plone.autoform.directives import read_permission
from plone.autoform.directives import write_permission
from plone.autoform.interfaces import IFormFieldProvider
from plone.behavior.interfaces import IBehavior
from plone.dexterity.behavior import DexterityBehaviorAssignable
from plone.dexterity.content import Item
from plone.dexterity.interfaces import IDexterityContent
from plone.namedfile import field as namedfile
from plone.restapi.tests.helpers import ascii_token
from plone.supermodel import model
Expand All @@ -17,6 +20,8 @@
from z3c.relationfield.schema import RelationChoice
from z3c.relationfield.schema import RelationList
from zope import schema
from zope.component import adapter
from zope.component import queryUtility
from zope.interface import directlyProvides
from zope.interface import implementer
from zope.interface import Invalid
Expand Down Expand Up @@ -317,3 +322,21 @@ class ITestBehavior(model.Schema):
@provider(IFormFieldProvider)
class ITestAnnotationsBehavior(model.Schema):
test_annotations_behavior_field = schema.TextLine(required=False)


class IInstanceBehaviorAssignableContent(IDexterityContent):
"""Marks dexterity content to be extensible by instance behaviors."""


@adapter(IInstanceBehaviorAssignableContent)
class DexterityInstanceBehaviorAssignable(DexterityBehaviorAssignable):
"""Per instance specification of plone.behavior behaviors.
- add tests.restapi.test_behavior
- remove plone.dublincore
"""

def enumerateBehaviors(self):
for behavior in super().enumerateBehaviors():
if behavior.name not in ["plone.dublincore"]:
yield behavior
yield queryUtility(IBehavior, name="tests.restapi.test_behavior")
26 changes: 26 additions & 0 deletions src/plone/restapi/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from plone.app.textfield import RichText
from plone.autoform import directives as form
from plone.dexterity.fti import DexterityFTI
from plone.dexterity.utils import createContentInContainer
from plone.restapi.testing import PLONE_RESTAPI_DX_INTEGRATION_TESTING
from plone.restapi.tests.dxtypes import IInstanceBehaviorAssignableContent
from plone.restapi.types.interfaces import IJsonSchemaProvider
from plone.restapi.types.utils import get_fieldsets
from plone.restapi.types.utils import get_jsonschema_for_fti
Expand All @@ -18,6 +20,7 @@
from z3c.form.browser.text import TextWidget
from zope import schema
from zope.component import getMultiAdapter
from zope.interface import alsoProvides
from zope.interface import provider
from zope.schema.interfaces import IContextAwareDefaultFactory
from zope.schema.vocabulary import SimpleTerm
Expand Down Expand Up @@ -129,6 +132,29 @@ def test_get_jsonschema_for_portal_type(self):
)
self.assertNotIn("title", list(jsonschema["properties"]))

def test_get_jsonschema_for_instance(self):
portal = self.portal
request = self.request
ttool = getToolByName(portal, "portal_types")
obj = createContentInContainer(
portal,
"Document",
id="doc",
title="Document",
)
jsonschema = get_jsonschema_for_fti(ttool["Document"], obj, request)
behaviors = [field["behavior"] for field in jsonschema["properties"].values()]
self.assertIn("plone.dublincore", behaviors)
self.assertNotIn("tests.restapi.test_behavior", behaviors)

# this testing adapter contextually add `test_behavior` and remove
# `dublincore`
alsoProvides(obj, IInstanceBehaviorAssignableContent)
jsonschema = get_jsonschema_for_fti(ttool["Document"], obj, request)
behaviors = [field["behavior"] for field in jsonschema["properties"].values()]
self.assertNotIn("plone.dublincore", behaviors)
self.assertIn("tests.restapi.test_behavior", behaviors)


class TestTaggedValuesJsonSchemaUtils(TestCase):

Expand Down
5 changes: 4 additions & 1 deletion src/plone/restapi/types/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,10 @@ def get_jsonschema_for_fti(fti, context, request, excluded_fields=None):
fieldsets = ()
additional_schemata = ()
else:
additional_schemata = tuple(getAdditionalSchemata(portal_type=fti.id))
if fti.id == context.portal_type:
additional_schemata = tuple(getAdditionalSchemata(context=context))
else:
additional_schemata = tuple(getAdditionalSchemata(portal_type=fti.id))
Copy link
Member

Choose a reason for hiding this comment

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

@mamico Can you help me understand the logic here? What is the scenario where we can't or shouldn't pass the context?

Copy link
Member Author

@mamico mamico Oct 17, 2024

Choose a reason for hiding this comment

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

@davisagli thanks for asking for clarification and putting me doubts...

The api is called to know the schema at the creation of a new CT or to know the schema at the modification of a content. In the first case I expected it to always be called from the root of the portal (but I'm wrong), in the second case it is called from the context of the object, and it would be useful to request the schema by passing the context.

So currently it is called (ie by Volto) as:
/++api++/@types/Document creation in the root
/++api++/folder/@types/Document creation in a folder
/++api++/folder/doc1/@types/Document modification of /folder/doc1

... but also ...

/++api++/folder/doc1/@types/Document to the creation of a new document inside doc1

... here the modification I proposed behaves incorrectly (doc1.portal_type is equal to Document but the correct schema is the generic schema, not the contextual).

I am putting the PR back in draft because it needs to be rethought in some way.

Suggestions?

Copy link
Member

Choose a reason for hiding this comment

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

@mamico It's important to get the schema for new content items in the context of its container, because some vocabularies might be context-dependent. (i.e. a list of keywords that depends on what section you are in)

So maybe we need a parameter so that the schema can be requested with or without context-dependent behaviors. Then they could be included by default, but the add form could request it without this.

fieldsets = get_fieldsets(context, request, schema, additional_schemata)

# Build JSON schema properties
Expand Down
Loading