Cannot import swagger 2.0 JSON file into smartdocs model

I am attempting to import a swagger 2.0 JSON file into an existing smartdoc model. I receive a "Your uploaded file does not appear to be a valid OpenAPI document." error.

I am able to validate the doc using 2 tools:

  1. https://swagger.io/tools/swagger-editor/
  2. https://bigstickcarpet.com/swagger-parser/www/index.html

but the import still fails with a message that the file does not appear to be an OpenAPI doc. I have gone as far as converting to "openapi": "3.0.0" and the file still fails.

screen-shot-2018-08-22-at-42427-pm.png

Any help to understand why the import fails is appreciated.

Solved Solved
0 3 475
1 ACCEPTED SOLUTION

@John Banning These are the relevant lines from smartdocs_import_model_validate():

$openapi = @json_decode($contents, TRUE);
$is_openapi = is_array($openapi) && array_key_exists('swagger', $openapi);

if (!$is_openapi) {
  form_set_error('file', t('Your uploaded file does not appear to be a valid OpenAPI document.'));
}

You should have a `swagger` directive in your openapi spec, not "openapi", that could be the problem. Another option is to convert to YAML and try uploading that way. Swagger.io's online editor can make that conversion for you easily.

View solution in original post

3 REPLIES 3

@John Banning These are the relevant lines from smartdocs_import_model_validate():

$openapi = @json_decode($contents, TRUE);
$is_openapi = is_array($openapi) && array_key_exists('swagger', $openapi);

if (!$is_openapi) {
  form_set_error('file', t('Your uploaded file does not appear to be a valid OpenAPI document.'));
}

You should have a `swagger` directive in your openapi spec, not "openapi", that could be the problem. Another option is to convert to YAML and try uploading that way. Swagger.io's online editor can make that conversion for you easily.

@Karl Scheirer I did some additional validation and it appeared to be an errant "," in the JSON.

So the @json_decode() failed until I fixed that.

Thanks for share the behinds the scene code in the smartdocs_import_model_validate()

How can we import a new swagger file into the existing smart doc?