Sling Model injection failures
Purpose: Diagnose models that adapt to null or render with empty fields.
Who this page is for
| Audience | Why it matters to you |
|---|---|
| Backend engineers | Daily dev-loop debugging |
| Frontend engineers | Reading the symptoms from HTL |
Two distinct symptoms
| Symptom | Meaning |
|---|---|
data-sly-use.model yields null (whole model absent) | Adaptation failed |
| Model present, some fields null/defaults | Individual injection failed (OPTIONAL strategy hides it) |
Adaptation returns null — checklist
- Registered?
/system/console/status-slingmodelslists all models — is yours there, for the right adaptable?
Missing ⇒ bundle's Sling-Model-Packages/annotation processing or bundle not Active (bundle page).
- Adaptable mismatch — model declares
Resourcebut HTL adapts from request (or vice versa).adaptables={…}can take both; injectors differ per adaptable (@ScriptVariable needs request). - A REQUIRED injection failed — default strategy without OPTIONAL: any missing property nulls the whole model. error.log at DEBUG for
org.apache.sling.modelssays exactly which injector failed — this log line is the single most useful fact on this page. - @PostConstruct threw — exception in error.log; model becomes null.
- Wrong class name in data-sly-use (package rename, typo) — HTL reports "identifier not found" style errors.
Fields empty though content exists
| Cause | Fix |
|---|---|
| Property name ≠ field name | @Named("jcr:title") etc. |
| Dialog saved to a different node/prefix | Inspect actual content in CRXDE vs model expectations (dialog issues) |
| Type mismatch (String saved, long injected) | TypeHints in dialog or String field + parse in @PostConstruct |
| @ChildResource shape mismatch | Multifield composite vs flat — align dialog and model |
| Injecting on request-adaptable via resource-only injector or vice versa | Match injector to adaptable |
Make it never happen again
OPTIONAL strategy + explicit defaults + a unit test with empty content (testing) turns this whole page from prod symptom into red test at build time.