Unable to adapt resource to my slingModel in test | Community
Skip to main content
Level 2
September 4, 2024
Solved

Unable to adapt resource to my slingModel in test

  • September 4, 2024
  • 5 replies
  • 1531 views

Hey , i have created json and i am trying to adapt resource to my sling model class. I have created the json correctly using .json on the browser with all fields of component dialog populated , however when i give resource path till the component my adapted sling model returns null . If i update the resource path to anything inside the component in json , i get an adapted sling model with null values for the fields inside (which is expected since the path is wrong).

My issue here is why is not adapting when i give correct path till the component in json . Is there something that i might be missing?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

5 replies

narendiran_ravi
Level 6
September 4, 2024

You can debug your Junit to see why the Sling Model has not been adopted. Check if there is any logic inside the @Postconstruct method of your Sling Model.

ZendarkkeAuthor
Level 2
September 4, 2024

There is logic within the post construct actually, so what do i have to do in this regard? The post construct method is retrieving a list of of child items present in dialog multifield and assigning to an array

arunpatidar
Community Advisor
Community Advisor
September 4, 2024
Kamal_Kishor
Community Advisor
Community Advisor
September 4, 2024

@zendarkke : Can you please share the code snippet of how you are resolving the resource. Also, if  possible the resource json.

EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
September 4, 2024

Hi,

The reason this is failing is that the "resource" (loaded through your JSON) does not meet all the requirements expected by your Sling model. You should check the following aspects of your Sling model:

  • Is it adaptable from a Resource or SlingServlet? How you are getting your slingModel in your test?
  • Is it tied to a resourceType?
  • Does it have all the required fields?
  • Does it depend on other OSGi services or objects that may not be available?
  • Etc.

A good way to figure out what is going on is to use the SlingModelFactory to create the Sling model instead of adapting it from the resource. This approach will provide a more informative error message about what failed during the model creation. Something like this:

MyModel model = context.getService(ModelFactory.class).createModel(request, MyModel.class);

 

Hope this helps.

Esteban Bustamante
kautuk_sahni
Community Manager
Community Manager
September 13, 2024

@zendarkke Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!

Kautuk Sahni