-1

Is there a component used to get the example or model from a swagger.json;

For example. This is a swagger.json: http://petstore.swagger.io/v2/swagger.json. I want to get the body example and body model of the interface: POST: /pet.

The body example is

    {
        "id": 0,
        "category": {
            "id": 0,
            "name": "string"
        },
        "name": "doggie",
        "photoUrls": [
            "string"
        ],
        "tags": [
            {
            "id": 0,
            "name": "string"
            }
        ],
        "status": "available"
    }
chen lin
  • 21
  • 4
  • What do you mean by "getting the model"? If you can see it you can also copy/paste it right? – Glenn Van Schil Jan 03 '18 at 10:41
  • @GlennVanSchil I Have re-edited the question. – chen lin Jan 03 '18 at 11:17
  • 1
    You don't mention in what language/framework you want to implement it and what you are trying to accomplish. This makes it very hard to help you since we don't know the use case – Glenn Van Schil Jan 03 '18 at 11:35
  • @GlennVanSchil I'm sorry that the language is javascript. – chen lin Jan 03 '18 at 11:43
  • Possible duplicate of [Generate sample request and responses from Swagger Definition](https://stackoverflow.com/questions/32430367/generate-sample-request-and-responses-from-swagger-definition) – Helen Jan 03 '18 at 12:25
  • What for? If you want to stub/mock the API, check out [Swagger mock servers](https://stackoverflow.com/q/38344711/113116). – Helen Jan 03 '18 at 12:26
  • @Helen I find a project: https://github.com/BigstickCarpet/swagger-parser. The apis that SwaggerParser.resolve() and SwaggerParser.parse() can resolve the problem,but it does not support 3.x – chen lin Jan 03 '18 at 12:54
  • What is your use case, exactly? A JavaScript client for an REST API? API mocking? Automated testing based on a Swagger spec? Something else? The more details you provide, the easier it is for others to suggest solutions. – Helen Jan 03 '18 at 13:13

1 Answers1

-1

Since the language is javascript you can just create a regular object

var myObject = {
  id: 0,
  category: {
    id: 0,
    name: "string"
  },
  name: "doggie",
  photoUrls: [
    "string"
  ],
  tags: [{
    id: 0,
    name: "string"
  }],
  status: "available"
};

You can also convert JSON strings to an object with

var myObject = JSON.parse(jsonString)

But swagger does not generate classes for you if that's what you're looking for.

Glenn Van Schil
  • 1,059
  • 3
  • 15
  • 33
  • 1
    In the swagger.json, the data structure is special and there is ref in the json. – chen lin Jan 03 '18 at 12:01
  • Swagger generates its JSON based on the objects from the back-end and I'm guessing that's what it's referring to, but take a look at the comment of Helen, maybe that's what you're looking for – Glenn Van Schil Jan 03 '18 at 12:38
  • I find a project: https://github.com/BigstickCarpet/swagger-parser. The apis that SwaggerParser.resolve() and SwaggerParser.parse() can resolve the problem,but it does not support 3.x. Thanks. – chen lin Jan 03 '18 at 12:54