Page MenuHomePhabricator

openapi-python-client generates exception with Wikibase REST API spec
Closed, InvalidPublicBUG REPORT

Description

I'm trying to generate a python client for the REST API using https://github.com/openapi-generators/openapi-python-client/

Getting it to work requires a few tweaks in the generator but I think there is also a bug in the actual openapi JSON.

The generator issued an exception when looking at the schema for Property (https://github.com/wikimedia/mediawiki-extensions-Wikibase/blob/e45d4f725e25a95ec2b034238fd3736f19053a16/repo/rest-api/specs/global/schemas.json).

For some reason the API specifies the statements of the Property differently from the statements of Item (which parses correctly by the tool). I think they should be the same? I was able to get the tool to work by just deleting the selection of the Property schema:

				"type": "object",
				"additionalProperties": {
					"type": "array",
					"items": {
						"allOf": [
							{ "$ref": "#/Statement" },
							{
								"properties": {
									"id": {
										"example": "P31$6403c562-401a-2b26-85cc-8327801145e1"
									}
								}
							}
						]
					}
				}

and replacing it with

				"type": "object",
				"additionalProperties": {
					"type": "array",
					"items": { "$ref": "#/Statement" }
				}

from the Item block above.

The id property that is being added seems redundant withe the id field in the Statement schema anyways? I don't think this is a breaking change.

Event Timeline

BrokenSegue renamed this task from REST API OpenAPI to REST API OpenAPI Redundant Schema .Jan 2 2024, 6:00 AM

Hi @BrokenSegue, the Property statements are specified that way so that the example shows a Property statement ID instead of an Item statement ID. If we make the two schemas the same, the examples will be the same as well, i.e. an Item statement ID for both, which is not ideal.

The schema as it is looks ok to me, and I'm sadly not sure what's causing the generator to throw an exception for you. Two things you could try are

  • using the built OpenAPI document instead of building it yourself
  • dereferencing the schema before generating the client

In case it helps, I have a little example repo for generating a TypeScript client here: https://github.com/jakobw/wb-rest-api-client-demo

Krinkle renamed this task from REST API OpenAPI Redundant Schema to openapi-python-client generates exception with Wikibase REST API spec .Jan 3 2024, 2:06 PM

Hey thanks for taking a look. I see I was unaware you could do that in a spec. It's cool that you provide a built version of the document (I wasn't able to find that before). Unfortunately it also fails on that version and dereferencing doesn't work either. Guess it's a bug in the generator (makes sense since it's not a very common pattern). I'm gonna just code this partially by hand I guess. Yeah I saw that there were some libraries for js/ts but I need a python library and I guess openapi support there is less mature.

Thanks!