OpenAPITools / OpenAPITools/openapi-generator

[BUG] [JAVA] Validate Required fields nullable

Open
#20,263 0 comments 0 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
  • [ X] Have you provided a full/minimal spec to reproduce the issue
  • [X ] Have you validated the input using an OpenAPI validator (https://editor.swagger.io/)
  • [ X] Have you tested with the latest master to confirm the issue still exists
  • [X ] Have you searched for related issues/PRs
  • [ X] What's the actual output vs expected output
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

In validateJsonElement method, during verify openapiRequiredFields jsonElement maybe JsonNull.
jsonElement.getAsJsonObject() is right for JsonObject not JsonElement.

if (jsonElement.getAsJsonObject().get(requiredField) == null) {
throw new IllegalArgumentException(String.format("The required field %s is not found in the JSON string: %s", requiredField, jsonElement.toString()));
}

openapi-generator version

7.10.0

OpenAPI declaration file content or url

openapi: 3.0.0
servers:

  • url: 'http://petstore.swagger.io/v2'
    info:
    version: 1.0.0
    title: OpenAPI Petstore
    tags:
  • name: pet
    description: Everything about your Pets
    paths:
    /pet:
    post:
    tags:
    - pet
    summary: Add a new pet to the store
    description: ''
    operationId: addPet
    responses:
    '200':
    description: successful operation
    content:
    application/xml:
    schema:
    $ref: '#/components/schemas/Pet'
    application/json:
    schema:
    $ref: '#/components/schemas/Pet'
    requestBody:
    $ref: '#/components/requestBodies/Pet'
    components:
    requestBodies:
    Pet:
    content:
    application/json:
    schema:
    $ref: '#/components/schemas/Pet'
    application/xml:
    schema:
    $ref: '#/components/schemas/Pet'
    description: Pet object that needs to be added to the store
    required: true
    schemas:
    Pet:
    title: a Pet
    description: A pet for sale in the pet store
    type: object
    required:
    - name
    - settings
    properties:
    id:
    type: integer
    format: int64
    name:
    type: string
    example: doggie
    settings:
    $ref: '#/components/schemas/settings'
    xml:
    name: Pet
    settings:
    title : settings
    nullable : true
    properties:
    id:
    type: integer
    format: int64
    name:
    type: string
    example: doggie
Generation Details

-g java
--additional-properties disallowAdditionalPropertiesIfNotPresent=false
-o build3
--strict-spec false
--skip-validate-spec

Steps to reproduce
Related issues/PRs
Suggest a fix

check jsonElement is JsonObject

if (jsonElement.getAsJsonObject().get(requiredField) == null) {
throw new IllegalArgumentException(String.format("The required field %s is not found in the JSON string: %s", requiredField, jsonElement.toString()));
}

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 by locating validateJsonElement in the Java generator output or its template, then reproduce the supplied OpenAPI Pet/settings case with the listed -g java options. Verify that a nullable required settings value is handled without treating JsonNull as a JsonObject, and confirm the generated client reports missing fields correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.