swagger-api / swagger-api/swagger-codegen

[JAVA] Problem generating template using the `required` array of object properties

Open
#5,737 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Java Issue: Bug Swagger-Parser
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

First of all thank you very much for the library.

Description

My end goal: To validate objects returned by my API. If they are marked as required in the required array property of an object, I want to fail validation if it does not exist in the response returned by the API call. Since the serializer is very lenient on json parsing, I'm trying to find another way to tell if an object in the response is missing altogether.

What I'm trying: I've modified pojo.mustache to use the {{#required}}...{{/required}} property to set an annotation on the property setter methods.

My problem: {{required}} is evaluating to false for properties that are object types even when the object is in the required list (see complex in definition file below). For array types and simple types it appears {{required}} correctly evaluates depending on the required list.

Swagger-codegen version

2.2.2

Swagger declaration file content or url
schema:
    type: object
    properties:
      data:
        type: object
        required:
          - complex
          - simple
        properties:
          complex:
            type: object
              properties:
                ...
          simple:
            type: string
Command line used for generation

Using the swagger-codegen-maven-plugin with default JAVA language options.

Steps to reproduce
  • Modify pojo.mustache to use the {{required}} variable
  • Generate JAVA client code
  • Note that properties that are object types are not being marked as required even if they are in the required list (e.g complex in the definition). Note that simple types do correctly appear required (e.g simple in the definition).
Suggest a Fix

It would be nice if I can know which properties are required or not when generating client code (including objects).

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 pojo.mustache template and reproduce the issue using the supplied YAML declaration through the swagger-codegen-maven-plugin with default JAVA options. Trace how the required value is provided for object properties versus simple and array properties; done means generating Java code where the object property in the required array is recognized as required.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
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.