swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.87k stars 6.02k forks source link

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

Open aaronschwartz opened 7 years ago

aaronschwartz commented 7 years ago

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
Suggest a Fix

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

wing328 commented 7 years ago

{{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.

@aaronschwartz I would suggest you to try the latest master as the issue may have been addressed.

You may also want to reference the object instead of defining the object inline.

aaronschwartz commented 7 years ago

I still see the same behavior using the latest pulled from master.

Defining the object as a $ref does seem to be a valid workaround.

Other types that are defined inline work as expected.

Most of my API isn't really reusable though and defining it inline is much more convenient. Also that would mean that I have to define every single object as a new model type, even if it is within another $ref.

wing328 commented 6 years ago

The latest master is using a newer version of Swagger Parser. Please give it a try to see if it helps.