Skip to content

[BUG][Ruby] Request body with array raises java.lang.NullPointerException #11158

@ykitamura-mdsol

Description

@ykitamura-mdsol

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Ruby client generation raises this exception:

Exception in thread "main" java.lang.RuntimeException: Could not generate api file for 'Pets'
	at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:678)
	at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:903)
	at org.openapitools.codegen.cmd.Generate.execute(Generate.java:441)
	at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
	at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Caused by: java.lang.NullPointerException
	at org.openapitools.codegen.languages.RubyClientCodegen.constructExampleCode(RubyClientCodegen.java:674)
	at org.openapitools.codegen.languages.RubyClientCodegen.constructExampleCode(RubyClientCodegen.java:683)
	at org.openapitools.codegen.languages.RubyClientCodegen.constructExampleCode(RubyClientCodegen.java:615)
	at org.openapitools.codegen.languages.RubyClientCodegen.postProcessOperationsWithModels(RubyClientCodegen.java:589)
	at org.openapitools.codegen.DefaultGenerator.processOperations(DefaultGenerator.java:1220)
	at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:581)
	... 4 more
openapi-generator version

5.3.0

OpenAPI declaration file content or url
openapi: '3.0.3'
info:
  version: 1.0.0
  title: Petstore
paths:
  /pets:
    post:
      summary: Create pets
      operationId: createPets
      tags:
        - pets
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Pets'
      responses:
        '201':
          description: Null response
components:
  schemas:
    Pets:
      type: array
      items:
        type: object
        properties:
          name:
            type: string
Generation Details

java -jar openapi-generator-cli-5.3.0.jar generate -i petstore.yaml -g ruby

Steps to reproduce

Use the generation details and the provided YAML file

Related issues/PRs

The generation doesn't fail with 5.2.1, it handles the array items as a number:

require 'time'
require 'openapi_client'

api_instance = OpenapiClient::PetsApi.new
request_body = [3.56] # Array<Object> | 

The changes in #10334 might cause this change.

The following languages also have the same issue:

  • crystal
  • go
  • powershell
Suggest a fix

Updating this part:

} else if (codegenProperty.isMap) {
return "{ key: " + constructExampleCode(codegenProperty.items, modelMaps, processedModelMap) + "}";

to something like:

        } else if (codegenProperty.isMap) {
            if (codegenProperty.items != null) {
                return "{ key: " + constructExampleCode(codegenProperty.items, modelMaps, processedModelMap) + "}";
            } else {
                return "{ ... }";
            }

will be a quick fix, it will generate clients successfully with examples like this:

require 'time'
require 'openapi_client'

api_instance = OpenapiClient::PetsApi.new
request_body = [{ ... }] # Array<Object> | 

(a long-term fix could be having codegenProperty.items properly when codegenProperty.isMap = True)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions