Page MenuHomePhabricator

SyntaxError: JSON Parse error: Expected '}' in StatementPanelConstructor
Closed, ResolvedPublicBUG REPORT

Description

New error in wmf10. 3,223 errors since deployment (triggering our email alert systems)
https://logstash.wikimedia.org/app/dashboards#/doc/logstash-*/logstash-default-1-7.0.0-1-2023.12.20?id=gkuoiYwBaR9sTI-qCy9-

Steps to replicate the issue (include links if applicable):

What happens?:

Error in JS console

What should have happened instead?:

No error in JS console.

Software version (skip for WMF-hosted wikis like Wikipedia):

Other information (browser name/version, screenshots, etc.):

Event Timeline

Restricted Application added a subscriber: Aklapper. · View Herald Transcript

Stack trace points to StatementPanelConstructor which has a JSON.parse without a try / catch block

Jdlrobson renamed this task from SyntaxError: JSON Parse error: Expected '}' to SyntaxError: JSON Parse error: Expected '}' in StatementPanelConstructor.Dec 21 2023, 6:31 PM

It looks like (at least most of them) are files with a "coordinate" statement.

E.g. view-source:https://commons.m.wikimedia.org/wiki/File:Found_an_old_Starbucks_logo_-starbucks_(16253373364).jpg

The data-formatvalue attribute for one particular property looks like this:

data-formatvalue="{"{\"value\":{\"entity-type\":\"property\",\"numeric-id\":1259,\"id\":\"P1259\"},\"type\":\"wikibase-entityid\"}":{"text\/html":{"en":{"":"<a target=\"_blank\" title=\"d:Special:EntityPage\/P1259\" href=\"https:\/\/www.wikidata.org\/wiki\/Special:EntityPage\/P1259\">coordinates of the point of view<\/a>"}},"text\/plain":{"en":{"":"coordinates of the point of view"}}},"{\"value\":{\"latitude\":49.288668,\"longitude\":-123.115109,\"altitude\":null,\"precision\":1.0e-6,\"globe\":\"http:\\\/\\\/www.wikidata.org\\\/entity\\\/Q2\"},\"type\":\"globecoordinate\"}":{"text\/html":{"en":{"P1259":"49\u00b017'19.205"N, 123\u00b06'54.392"W"}},"text\/plain":{"en":{"P1259":"49\u00b017'19.205\"N, 123\u00b06'54.392\"W"}}}}"

Which becomes available in JS like so:

`{"{\\"value\\":{\\"entity-type\\":\\"property\\",\\"numeric-id\\":1259,\\"id\\":\\"P1259\\"},\\"type\\":\\"wikibase-entityid\\"}":{"text\\/html":{"en":{"":"<a target=\\"_blank\\" title=\\"d:Special:EntityPage\\/P1259\\" href=\\"https:\\/\\/www.wikidata.org\\/wiki\\/Special:EntityPage\\/P1259\\">coordinates of the point of view<\\/a>"}},"text\\/plain":{"en":{"":"coordinates of the point of view"}}},"{\\"value\\":{\\"latitude\\":49.288668,\\"longitude\\":-123.115109,\\"altitude\\":null,\\"precision\\":1.0e-6,\\"globe\\":\\"http:\\\\\\/\\\\\\/www.wikidata.org\\\\\\/entity\\\\\\/Q2\\"},\\"type\\":\\"globecoordinate\\"}":{"text\\/html":{"en":{"P1259":"49\\u00b017'19.205"N, 123\\u00b06'54.392"W"}},"text\\/plain":{"en":{"P1259":"49\\u00b017'19.205\\"N, 123\\u00b06'54.392\\"W"}}}}`

The malformed bit are the " (double quotes) in the coordinates part of this snippet {"text\\/html":{"en":{"P1259":"49\\u00b017'19.205"N, 123\\u00b06'54.392"W"}}


Here's a quick simulation of how we come up with this data in the first place:

$mediaInfoId = new Wikibase\MediaInfo\DataModel\MediaInfoId( 'M75077787' );
$entityLookup = Wikibase\Repo\WikibaseRepo::getEntityLookup();
$entity = $entityLookup->getEntity( $mediaInfoId );
$statements = $entity->getStatements();

$propertyIdString = 'P1259';
$propertyId = new Wikibase\DataModel\Entity\NumericPropertyId( $propertyIdString );
$statement = $statements->getByPropertyId( $propertyId )->toArray()[0];

$snak = $statement->getMainSnak();

$serializer = new DataValues\Serializers\DataValueSerializer();
$serialized = $serializer->serialize( $snak->getDataValue() );
$data = json_encode( $serialized );
$property = $snak->getPropertyId()->getSerialization();
$snakFormatterFactory = Wikibase\Repo\WikibaseRepo::getSnakFormatterFactory();

$result = [];
$formatter = $snakFormatterFactory->getSnakFormatter( 'text/html', new ValueFormatters\FormatterOptions( [ 'lang' => 'en' ] ) );
$result[$data]['text/html']['en'][$propertyIdString] = $formatter->formatSnak( $snak );

$formatter = $snakFormatterFactory->getSnakFormatter( 'text/plain', new ValueFormatters\FormatterOptions( [ 'lang' => 'en' ] ) );
$result[$data]['text/plain']['en'][$propertyIdString] = $formatter->formatSnak( $snak );

var_dump( serialize( $result ) );

Which generates (and then gets added to PanelLayout via ->setAttributes):

string(385) "a:1:{s:164:"{"value":{"latitude":49.288668,"longitude":-123.115109,"altitude":null,"precision":1.0e-6,"globe":"http:\/\/www.wikidata.org\/entity\/Q2"},"type":"globecoordinate"}";a:2:{s:9:"text/html";a:1:{s:2:"en";a:1:{s:5:"P1259";s:52:"49°17&apos;19.205&quot;N, 123°6&apos;54.392&quot;W";}}s:10:"text/plain";a:1:{s:2:"en";a:1:{s:5:"P1259";s:32:"49°17'19.205"N, 123°6'54.392"W";}}}}"

As we can see, these quotation marks are already HTML-escaped here (&quot;).
In the actual output that's generated, they remain as they already were, but they should probably get another round of escaping (&amp;quot;)
I'm not yet sure what/where something changed that seems to have caused this.

I think this patch is related - the error seems gone when this one is reverted: https://github.com/wikimedia/mediawiki/commit/84d0dff9680bed2aeaa491dfb5958634e0f27ec6

Have to leave now, will be back in 1-2hr.

Looks like it was actually https://github.com/wikimedia/mediawiki/commit/82da9cf14be08e9458f58fa96be51966a2fe7cb1 (which began to use the code introduced in https://github.com/wikimedia/mediawiki/commit/84d0dff9680bed2aeaa491dfb5958634e0f27ec6) that caused this.

Said patch has already been reverted (for T353920), which should also resolve this UBN.

matthiasmullie claimed this task.

Since that revert was deployed, https://commons.m.wikimedia.org/wiki/File:Found_an_old_Starbucks_logo_-starbucks_(16253373364).jpg, a page where this error used to occur, seems fine.

https://logstash.wikimedia.org/goto/c549b19bbf2741a306a15e7ec3f53975 also shows conclusively that these errors have stopped.

Thank you fixing the issue and your thorough write up!!