• Resolved Gerald

    (@gerital)


    I registered a custom REST API endpoint to WordPress and it shows up correctly at the Swagger Docs page.

    My API uses query parameters and I would like to add them to the parameters section of the WordPress Swagger Docs page.

    I know to do that with plain Swagger, but I’m confused about how to define that parameters in WordPress so the get used by Swagger.

    Any hint how to do that?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Gerald

    (@gerital)

    Some additional info on the problem:

    1. The referred Swagger Docs page is here: https://test.xrcb.cat/ca/rest-api/docs/
    2. The API works fine, for example using these requests:
      https://test.xrcb.cat/ca/wp-json/xrcb/v1/radios/
      https://test.xrcb.cat/ca/wp-json/xrcb/v1/radios/2500
    3. I tried to add a ‘schema’ parameter to the register_rest_route args parameter, but without success.
    Thread Starter Gerald

    (@gerital)

    Ok, finally I resolved the problem, I didn’t specify the args parameter at register_rest_route.

    The correct way to register an endpoint with a path parameter to point to a specific resource within a collection, in my case a radio identified by its ID, is like that:

    add_action( 'rest_api_init', function () {
    	register_rest_route( 'xrcb/v1', '/radios/(?P<id>\d+)?', array(
    		'methods' => 'GET',
    		'callback' => 'xrcbapi_radios_api_endpoint',
    		'args' => array(
    			'id' => array(
    		        'validate_callback' => function($param, $request, $key) {
    		        	return is_numeric( $param );
    		        }
    		    ),
    		),
    	));
    });

    Now the parameter shows up at the swagger docs page.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to add parameter documentation’ is closed to new replies.