swagger-api / swagger-api/swagger-parser

How to get the body payload?

Open
#544 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
867
Forks
560
Avg merge
2d 21h
Merged PRs (30d)
7

Description

I am hoping this is a defect against documentation.

I am unable to retrieve the body payload for a POST operation.

Using the Swagger-Petstore sample, the POST to /pet has a body parameter defined as:

{
  "in": "body",
  "name": "body",
  "description": "Pet object that needs to be added to the store",
  "required": true,
  "schema": {
     "$ref": "#\/definitions\/Pet"
  }
}

Here is my sample code:

import io.swagger.models.Operation;
import io.swagger.models.Path;
import io.swagger.models.Swagger;
import io.swagger.models.parameters.Parameter;
import io.swagger.parser.SwaggerParser;

public class PetPostBody {

    public static void main(String[] args) {
	Swagger swagger = new SwaggerParser().read("http://petstore.swagger.io/v2/swagger.json");

	Path pet = swagger.getPath("/pet");
	Operation post = pet.getPost();
	Parameter body = null;
	for (Parameter param : post.getParameters()) {
	    if (param.getIn().equals("body"))
		body = param;
	}
	body.get???
    }
}

How do I retrieve the payload?

The only things that I can see (in debugger) for body is schema but that is access protected(?), and under that is genericRef (access private) that points to the definitions.

What am I missing?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Parameter, Operation, and SwaggerParser APIs used in the example, along with the Swagger-Petstore /pet definition. Determine the documented way to inspect a POST body parameter and its schema, then add a concise usage example and verify it against the referenced specification.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.