OpenAPITools / OpenAPITools/openapi-generator

[BUG] Mapping various number types (with formats) to primitives does not seem to work & Lists not instantiated

Open
#12,139 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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)?
  • 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?
Description
  1. I want to map various Java wrapper objects to their corresponding primitives (int32 -> int, not Integer, int64 -> long, not Long, boolean -> boolean, not Boolean)
  2. Also trying to have lists instantiated by default with an empty list, not null.

According to the documentation, this should be possible - so maybe it's not a bug but I'm just doing something wrong

openapi-generator version

5.4.0

Generation Details

My configuration:

  <configuration>
                            <generatorName>java</generatorName>
                            <additionalProperties>removeEnumValuePrefix=false</additionalProperties>
                            <typeMappings>integer+int64=long,boolean=boolean</typeMappings>
                            <skipValidateSpec>true</skipValidateSpec>
                            <instantiationTypes>array=ArrayList</instantiationTypes>
</configuration>

Steps to reproduce
---
openapi: 3.0.3
info:
  title: API
  version: 1.0.0-SNAPSHOT
paths:
  '/serverInfo':
    get:
      summary: Get server info
      description: Get server info
      operationId: getServerInfo
      responses:
        '200':
          description: OK

components:
  schemas:
    ErrorResponse:
      required:
        - code
      type: object
      properties:
        code:
          type: integer
          format: int32
        messages:
          type: array
          items:
            type: string

I followed the documentation for the type mappings but the end result is always:

private Integer code; and not private int code; - which I would like
private List<String> messages = null; instead of List<String> messages = new Arraylist<>();

Note 1 : it works for boolean
Note 2 : Configuration is on plugin level, not execution level

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 Java generator's handling of the typeMappings and instantiationTypes configuration shown in the issue, then reproduce the behavior with the supplied OpenAPI 3.0.3 specification. Check the generated ErrorResponse fields against the requested primitive code type and initialized messages list; done means both mappings produce the expected declarations and existing behavior remains intact.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.