[BUG][GO] Generator throws a NullPointerException when using a required property that is a reference to a oneOf
Created by: jagobagascon
Description
I'm trying to generate a simple model that references an external 'oneOf' property but the generator is throwing the following exception:
Exception in thread "main" java.lang.RuntimeException: Could not generate api file for 'Default'
at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:675)
at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:879)
at org.openapitools.codegen.cmd.Generate.execute(Generate.java:432)
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.GoClientCodegen.constructExampleCode(GoClientCodegen.java:591)
at org.openapitools.codegen.languages.GoClientCodegen.constructExampleCode(GoClientCodegen.java:607)
at org.openapitools.codegen.languages.GoClientCodegen.constructExampleCode(GoClientCodegen.java:572)
at org.openapitools.codegen.languages.GoClientCodegen.constructExampleCode(GoClientCodegen.java:615)
at org.openapitools.codegen.languages.GoClientCodegen.constructExampleCode(GoClientCodegen.java:509)
at org.openapitools.codegen.languages.GoClientCodegen.postProcessOperationsWithModels(GoClientCodegen.java:446)
at org.openapitools.codegen.DefaultGenerator.processOperations(DefaultGenerator.java:1195)
at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:580)
... 4 more
The error only happens when the oneOf
only contains primitive types or empty object types. If I uncomment the code in this example the error just dissapears.
schemas:
pet:
title: Pet
properties:
pet_type:
$ref: "#/components/schemas/pet_type"
required:
- pet_type
pet_type:
oneOf:
- type: string
- type: integer
# adding this removes the error
# - type: object
# properties:
# name:
# type: string
openapi-generator version
5.0.1
OpenAPI declaration file content or url
openapi: "3.0.0"
info:
version: 1.0.0
title: Petstore
paths:
/pets:
post:
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/pet"
responses:
200:
description: pet response
components:
schemas:
pet:
title: Pet
properties:
pet_type:
$ref: "#/components/schemas/pet_type"
required:
- pet_type
pet_type:
oneOf:
- type: string
- type: integer
Steps to reproduce
Just try to generate the previous yaml
using the go generator:
openapi-generator-cli generate -i spec.yaml -g go -o out
Related Issues
I think this error is related to #8769