Skip to content

Commit 97ee84a

Browse files
Fix undefined array key 1 error in sectionsWithAppBlock (#448)
* fix(theme): allow hyphenated schema tags and guard against missing match * remove unnecessary line
1 parent 8a1b3b4 commit 97ee84a

3 files changed

Lines changed: 39 additions & 4 deletions

File tree

src/Services/ThemeHelper.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,11 @@ public function sectionsWithAppBlock(array $templateMainSections): array
208208
$asset = $assetResponse['body']['asset']->toArray();
209209

210210
preg_match('/\{\%\s+schema\s+\%\}([\s\S]*?)\{\%\s+endschema\s+\%\}/m', $asset['value'], $matches);
211-
$schema = json_decode($matches[1], true);
212-
213-
if ($schema && isset($schema['blocks'])) {
214-
$acceptsAppBlock = in_array('@app', array_column($schema['blocks'], 'type'));
211+
if (!empty($matches) && isset($matches[1])) {
212+
$schema = json_decode($matches[1], true);
213+
if ($schema && isset($schema['blocks'])) {
214+
$acceptsAppBlock = in_array('@app', array_column($schema['blocks'], 'type'));
215+
}
215216
}
216217

217218
return $acceptsAppBlock ? $file : null;

tests/Services/ThemeHelperTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,32 @@ public function testSectionsWithAppBlock(): void
127127

128128
$this->assertNotEmpty($sectionsWithApp);
129129
}
130+
131+
public function testSectionsWithAppBlockHandlesMissingSchema(): void
132+
{
133+
// Response stubbing
134+
$this->setApiStub();
135+
ApiStub::stubResponses([
136+
'get_themes',
137+
'get_theme_assets',
138+
'get_theme_asset_no_schema',
139+
]);
140+
141+
// Define config to include a template
142+
$this->app['config']->set(
143+
'shopify-app.theme_support.templates',
144+
[
145+
'asset',
146+
]
147+
);
148+
149+
// Run extraction
150+
$this->helper->extractStoreMainTheme($this->shop->getId());
151+
$jsonFiles = $this->helper->templateJSONFiles();
152+
$mainSections = $this->helper->mainSections($jsonFiles);
153+
$sectionsWithApp = $this->helper->sectionsWithAppBlock($mainSections);
154+
155+
// Assert no sections are returned and no error occurs
156+
$this->assertEmpty($sectionsWithApp);
157+
}
130158
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"asset": {
3+
"key": "sections/no-schema.liquid",
4+
"value": "<!-- Invalid schema -->"
5+
}
6+
}

0 commit comments

Comments
 (0)