Page MenuHomePhabricator

Wikibase REST API Swagger docs are missing "type: statement" on statements POST endpoint
Closed, ResolvedPublicBUG REPORT

Description

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

  • View the REST API Swagger docs at https://doc.wikimedia.org/Wikibase/master/js/rest-api/
  • See the format defined in the docs for creating a new statement with the POST /entities/items/{item_id}/statements endpoint.
  • Try to post the first JSON blob below to an item on the beta Wikidata site.
  • See that it errors with { "code": "invalid-statement-data", "message": "Invalid statement data provided" }, despite having all the components that are declared in the Swagger docs.

This is what I tried POSTing to the endpoint, which failed:

{
  "statement": {
    "mainsnak": {
      "snaktype": "value",
      "property": "P625",
      "datatype": "string",
      "datavalue": {
        "type": "string",
        "value": "test data"
      }
    }
  },
  "bot": true,
  "tags": [],
  "comment": "foo"
}

This is what the endpoint *actually* wanted:

{
  "statement": {
    "mainsnak": {
      "snaktype": "value",
      "property": "P625",
      "datatype": "string",
      "datavalue": {
        "type": "string",
        "value": "test data"
      }
    },
    "type": "statement"
  },
  "bot": true,
  "tags": [],
  "comment": "foo"
}

The only difference is the "type": "statement" line, which the Swagger docs do not mention/include. I only figured this out after going into the Wikibase codebase and browsing the unit tests. 😅

What happens?:

An error about an invalid statement is returned and the Swagger docs make no mention of a type attribute in the statement JSON.

What should have happened instead?:

The Swagger docs for the statements POST endpoint should include the required type key.

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

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

If the same type attribute should be used on the PUT endpoint(s) for statements, those should be updated as well.

Event Timeline

I managed to figure out how to generate the Swagger docs locally, and changing repo/rest-api/specs/global/request-parts.json in the Wikibase repo fixes this problem:

{
	"StatementRequest": {
		"allOf": [
			{
				"type": "object",
				"properties": {
					"statement": {
						"allOf": [
							{ "$ref": "./schemas.json#/Statement" },
							{ "$ref": "#/StatementRequestRequired" }
						]
					}
				},
				"required": [ "statement" ]
			},
			{ "$ref": "./request-parts.json#/MediawikiEdit" }
		]
	},
	"MediawikiEdit": {
		"type": "object",
		"properties": {
			"tags": {
				"type": "array",
				"items": { "type": "string" },
				"default": [ ]
			},
			"bot": {
				"type": "boolean",
				"default": false
			},
			"comment": {
				"type": "string"
			}
		}
	},
	"QualifiersRequestRequired": {
		"additionalProperties": {
			"items": {
				"required": [ "snaktype", "property" ]
			}
		}
	},
	"ReferenceRequestRequired": {
		"required": [ "snaks", "snaks-order" ],
		"properties": {
			"snaks": {
				"additionalProperties": {
					"items": {
						"required": [ "snaktype", "property" ]
					}
				}
			}
		}
	},
	"ReferencesRequestRequired": {
		"items": { "$ref": "#/ReferenceRequestRequired" }
	},
	"StatementRequestRequired": {
		"required": [ "mainsnak", "type" ],
		"properties": {
			"mainsnak": { "required": [ "snaktype", "property" ] },
			"qualifiers": { "$ref": "#/QualifiersRequestRequired" },
			"references": { "$ref": "#/ReferencesRequestRequired" },
			"type": {
				"type": "string",
				"example": "statement"
			}
		}
	}
}

Pretty much just added the type property at the bottom and made type a required property. I'd contribute this myself, but I have no idea how to use Gerrit and I assume it'll probably need some changes after code review, so I'll let someone more familiar handle things :)

Note that this also changes the PUT request for replacing statements, not 100% sure if that's correct?

Hi, thanks for submitting this task and sorry this wasn't better documented!

We were discussing removing the "type" field altogether which is why the Swagger docs and implementation don't quite align. Your ticket prompted us to create T316077 to address this.

"type" field has been removed from requests and responses (see T316077) which makes the issue resolved I'd think.

WMDE-leszek claimed this task.