How to use expansions on ContentPropertyService?


I’m using ScriptRunner to create some properties for a page, which works ok. What I can’t seem to do is use .find to return them as Groovy’s proxy class blocks .find unless I explicitly set an Expansion; however I can’t figure out the correct expansions to set.

@Field ContentPropertyService contentPropertyService = ComponentLocator.getComponent ContentPropertyService;

ContentId pageId = ContentId.of(Long.valueOf('19071593');
String myKey = 'my-key';
JsonContentProperty newProperty = JsonContentProperty.builder()
   .value(new JsonString('{\'when\': \'2021-04-08\'}'))

works, and I can see the property exists by hitting the rest endpoint

{"id":"19071593", "key":"my-key", "value":{'when': '2021-04-08'}, "version...}

but if I try getting the content from the service instead of the REST api, I get cannot call get() on collapsed object

def found = contentPropertyService.find(new Expansion(JsonContentProperty.Expansions.CONTENT), new Expansion('property'))

 log.warn "found: ${found.get()"

So what are the correct expansions to pass in? I can’t do contentPropertyService.find() because Groovy gets upset as it creates a proxy that doesn’t have access to the finder interface, so I believe I’ve got to use expansion

Answering myself in case anyone else has this issue

def found = contentPropertyService.find(new Expansion('content'), new Expansion('version')
log.warn found.get().value

gets the values.

I don’t suppose the Java APIs can have some more involved examples so that people don’t spend a day trying to use the ‘new’ APIs?

1 Like

Thank you so much. Yes, it seems Atlassian didn’t document at all the use of ContentService or how to update the body of a page.

The thing to remember is, it works like in the REST API. Imagine you want to get the storage format of a page, and you would write the URL with ? - You would get the same with:

    new Expansion("body", new Expansions(new Expansion("storage")))

@aragot , I’ve recently found that all these new API services can be easily loaded via the ScriptRunner @PluginModule annotation, making usage a little easier.

ContentService contentService

for example, however you must using the Expansions on the find else Groovy’s proxy class loader goes haywire. Unfortunately the REST API documentation doesn’t give comprehensive information on expansion options; some Services contain a static default set, but it appears for the majority, it’s a voyage of discovery on the REST API version first…

Your note on how to use the compound Expansions is very useful - I’d not noted the Expansions class in the Javadoc.