OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] RequestBody examples not set in docs

Open
#6,365 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Java Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

Generated examples in docs don't include sample body parameters. For example this is the output generated for the openapi declaration included below in the post:

// Import classes:
import org.openapitools.client.ApiClient;
import org.openapitools.client.ApiException;
import org.openapitools.client.Configuration;
import org.openapitools.client.models.*;
import org.openapitools.client.api.DefaultApi;

public class Example {
    public static void main(String[] args) {
        ApiClient defaultClient = Configuration.getDefaultApiClient();
        defaultClient.setBasePath("https://petstore.swagger.io/v2");

        DefaultApi apiInstance = new DefaultApi(defaultClient);
        Pet pet = new Pet(); // Pet | 
        try {
            Pet result = apiInstance.createPet(pet);
            System.out.println(result);
        } catch (ApiException e) {
            System.err.println("Exception when calling DefaultApi#createPet");
            System.err.println("Status code: " + e.getCode());
            System.err.println("Reason: " + e.getResponseBody());
            System.err.println("Response headers: " + e.getResponseHeaders());
            e.printStackTrace();
        }
    }
}

The expected output would be to include the example parameters:

Pet pet = new Pet();
pet.setName("MyPet");
pet.setPetType("Cat");
openapi-generator version

Tried with 5.0.0-snapshot and the stable 4.3.1 release.

OpenAPI declaration file content or url
openapi: 3.0.2
info:
  version: 1.0.0
  title: PetStore API
servers:
  - url: https://petstore.swagger.io/v2
    description: PetStore server
paths:
  /pets:
    post:
      summary: Create a Pet
      operationId: createPet
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Pet"

      responses:
        201:
          description: Created
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pet"

components:
  schemas:
    Pet:
      type: object
      properties:
        name:
          type: string
        petType:
          type: string
      required:
        - name
        - petType
      example:
        name: "MyPet"
        petType: "Cat"
Command line used for generation
java -DhideGenerationTimestamp=true -jar ./scripts/openapi-generator-cli.jar generate \
    -i ./openapi/openapi.yaml \
    -g java \
    --library resteasy \
    -o ./clients/swagger-java \
    -c ./scripts/config.json
Steps to reproduce
  1. Add an example directive to a RequestBody or to any other object defining example values.
  2. Observe the docs, for example, DefaultApi.md will not have the example value set in the RequestBody Object. It will only work for primitives.
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/6324

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 provided OpenAPI declaration and generation command using scripts/openapi-generator-cli.jar, then inspect the generated DefaultApi.md. Compare the RequestBody documentation with the schema example and expected Java assignments; done means generated docs include object example values, not only primitive examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, documentation
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.