Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when providing old apidoc apiExample syntax #4

Open
quorak opened this issue Oct 19, 2018 · 8 comments
Open

Error when providing old apidoc apiExample syntax #4

quorak opened this issue Oct 19, 2018 · 8 comments

Comments

@quorak
Copy link
Contributor

quorak commented Oct 19, 2018

hey there,
thx for the plugin.

when i mix old and new syntax in different blocks, I get error:
error: Cannot read property 'path' of null

Code:

/**
 * @api {get} /user/:id
 * @apiExample {curl} Example usage:
 *     curl -i http://localhost/user/4711
 */
[...]
/**
 * @api {get} /api GetAPI
 * @apiExample {json=./ex/api.req.json} apiParamExample Request
 */
@willfarrell
Copy link
Owner

I'm not familiar with this old syntax you're referring too. Possible you have more than one apiExample plugin connected? If so they will conflict.

@quorak
Copy link
Contributor Author

quorak commented Oct 30, 2018

with old syntax, I mean the standard syntax, without the plugin. see http://apidocjs.com/#param-api-example

@Arxcis
Copy link

Arxcis commented Mar 6, 2019

Bump. This plugin does not allow to mix @apiExample {curl} and @apiExample {json}.
My use-case:

/**
 * @api {get} /user/:id User
 * @apiGroup Users
 * @apiSchema {jsonschema=./schema.json} apiSuccess
 * @apiExample {curl}  Example usage:
 *     curl -i http://localhost/user/4711 example Request
 * @apiExample {jsonschema=./schema.json} apiSuccessExample Response JSON
 */

@Arxcis
Copy link

Arxcis commented Mar 6, 2019

The bug is because parse() in parser/api_example.js fails, and the caller parserExampleElements() in index.js does not check for null

function parse(content) {
  // ...
  if ( ! matches)
    return null;
  // ...
}

Recommended solution is to check if values is null

function parserExampleElements(elements, element, block, filename) {
  // ...
  const values = elementParser.parse(element.content, element.source);
  if (!values) {   // < -----------------  Should check if values null --------
      return elements
  }
  // ....
}

@Arxcis
Copy link

Arxcis commented Mar 6, 2019

Should probably also make the regex more specific, since only json, jsonschema and xml is supported

I suggest changing

  var parseRegExp = /^\{(.+?)=(.+?)\}\s*(?:\s+(.+?))?\s(.+?)$/g;

...to

  var parseRegExp = /^\{\s*(json|jsonschema|xml)\s*=(.+?)\}\s*(?:\s+(.+?))?\s(.+?)$/g;

@willfarrell
Copy link
Owner

@Arxcis nice catch, would you like to put a PR together for that?

@Arxcis
Copy link

Arxcis commented Mar 18, 2019

@willfarrell Alright, will do. I am not sure if just returning elements is correct thing to do, or if I have to do .push() or .pop() also? Do you know?

Arxcis added a commit to Arxcis/apidoc-plugin-example that referenced this issue Mar 21, 2019
Arxcis added a commit to Arxcis/apidoc-plugin-example that referenced this issue Mar 21, 2019
- This makes it possible to play more nicely with other plugins like {curl=example}
@canonex
Copy link

canonex commented Nov 29, 2021

Hi, is this merged with the npm version?
I get the same error.

Thank you,
Riccardo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants