mohsen1 / multi-file-swagger-example

Multi-file Swagger example
http://azimi.me/2015/07/16/split-swagger-into-smaller-files.html
168 stars 39 forks source link

Running into issue where my resolved JSON is not converted to correct YAML file. #13

Open ngoctranfire opened 7 years ago

ngoctranfire commented 7 years ago
swagger: '2.0'
info: {version: 0.0.1, title: KCC APi}
host: localhost:3000
basePath: /api/v1
schemes: [http, https]
consumes: [application/json]
produces: [application/json]
paths:
  /hello:
    x-swagger-router-controller: hello_world
    get:
      description: Returns the listing associated with this id
      operationId: listing
      parameters:
      - {name: itemName, in: body, description: The name of the item, required: true,
        type: string}
      - {name: itemUrl, in: body, description: The link to the item url, required: true,
        type: string}
      - {name: itemDescription, in: body, description: The item description, type: string,
        required: true}
      - {name: designer, in: body, description: The item's designer, type: string,
        required: true}
      - {name: imageUrl, in: body, description: The name of the person to whom to
          say hello, required: false, type: string}
      - {name: expireDate, in: body, description: The expiry date of the, type: long,
        required: true}
      responses:
        '200':
          description: Success
          schema: {$ref: '#/definitions/HelloWorldResponse'}
        default:
          description: Error
          schema: {$ref: '#/definitions/ErrorResponse'}
  /listing:
    /add:
      x-swagger-router-controller: listing
      post:
        description: Returns the listing associated with this id
        operationId: listing
        parameters:
        - {name: itemName, in: body, description: The name of the item, required: true,
          type: string}
        responses:
          '200':
            description: Success Response
            schema: {$ref: '#/definitions/SuccessResponse'}
          default:
            description: Error Response
            schema: {$ref: '#/definitions/ErrorResponse'}
      /swagger: {x-swagger-pipe: swagger_raw}
    /get: null
    /list: null
  /swagger: {x-swagger-pipe: swagger_raw}
definitions:
  SuccessResponse:
    required: [ok]
    properties:
      message: {type: boolean}
  ErrorResponse:
    required: [message]
    properties:
      message: {type: string}

Taking a look above, this is the resolved, using the following code:


const resolve = require('json-refs').resolveRefs;
const YAML = require('yaml-js');
const fs = require('fs');

const root = YAML.load(fs.readFileSync('api/swagger/index.yaml').toString());
const options = {
  filter        : ['relative', 'remote'],
  loaderOptions : {
    processContent : function (res, callback) {
      callback(null, YAML.load(res.text));
    }
  },
  relativeBase: "api/swagger",
};

resolve(root, options).then(function (results) {
  console.log(YAML.dump(results.resolved));
});```
mohsen1 commented 7 years ago

resolve({definitions: root},... JSON Ref API has changed.

ngoctranfire commented 7 years ago

I tried that and the same thing happens where my results are still not resolved correctly.

ngoctranfire commented 7 years ago

Also, adding that resolve({definitions: root}), causes it add an outer definitions that I don't need/want.

tom-lord commented 7 years ago

@mohsen1 I don't really understand your comment above. I'm running into the exact same issue, where opening a swagger project from the generated json file gives the error:

options.definition must be either an object or a string

I also get that same error when trying to load the json file that is generated by this project.

This issue in swagger-editor appears to be the same problem.