Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] Enum in array of array #66

Merged
merged 7 commits into from
May 18, 2018
Merged

[java] Enum in array of array #66

merged 7 commits into from
May 18, 2018

Conversation

jmini
Copy link
Member

@jmini jmini commented May 16, 2018

Fix for #16


This PR introduces CodegenProperty.mostInnerItems and CodegenParameter.mostInnerItems that gives always the most inner items type when arrays are contained in arrays.

OAS2 (extract):

definitions:
  SomeObj:
    type: object
    properties:
      order:
        type: array
        items:
          type: array
          items:
            type: string
            enum:
              - val1
              - val2
              - val3

Example in Java:
The CodegenProperty created for order defines the type List<List<OrderEnum>>.

Before this PR, if you want to access the CodegenProperty corresponding to the enum you need to do items.items (because we have an array of array of enum). The problem is that you do not know how many time you need to loop. If you just have a simple array of enum it will be items

In the Java code you often see this pattern:

                while (codegenProperty != null) {
                    codegenProperty = codegenProperty.items;
                }

To find the most inner items CodegenProperty

This PR introduces mostInnerItems that you can call at top level (you should check isContainer) and that gives you the items you are looking for (the one corresponding to OrderEnum in the example)

@jmini jmini changed the title Fix for #16 [java] Enum in array of array May 16, 2018
@jmini
Copy link
Member Author

jmini commented May 16, 2018

I could not use the gist provided by @msjoshi in the original issue swagger-api/swagger-codegen#7918 because of #50. So I tested this PR with this OpenAPI version 2 Specification:

swagger: '2.0'
info:
  title: Patrocinium
  description: Patrocinium API
  version: 1.0.0
  termsOfService: 'https://www.patrocinium.com/end-user-licensing-agreement/'
  contact:
    name: Patrocinium
    url: 'https://www.patrocinium.com'
    email: [email protected]
host: papi.patrocinium.com
basePath: /v1
schemes:
  - https
  - http
consumes:
  - application/json
produces:
  - application/json
paths:
  /db/ActionRole/findAndCountAll:
    post:
      tags:
        - db
      description: Find all instances of ActionRole
      operationId: findActionRole
      parameters:
        - in: body
          name: body
          required: false
          schema:
            $ref: '#/definitions/ActionRoleSearch'
      responses:
        '200':
          description: Successful Operation
        '400':
          description: Bad Request
        '403':
          description: Unauthorized
        '500':
          description: Internal Server Error
definitions:
  ActionRoleSearch:
    type: object
    properties:
      attributes:
        description: A list of the attributes that you want to return.
        type: array
        items:
          type: string
          enum:
            - id
            - actionId
            - roleId
            - createdAt
            - updatedAt
            - deletedAt
      order:
        description: A collection of grouped attribute names and directions.
        type: array
        items:
          type: array
          items:
            type: string
            enum:
              - id
              - actionId
              - roleId
              - createdAt
              - updatedAt
              - deletedAt
              - ASC
              - DESC
      limit:
        description: The number of ActionRole instances to return.
        type: integer
        format: int64
        default: 100
        minimum: 1
        maximum: 1000
      offset:
        description: The number of ActionRole instances to offset the returned rows.
        type: integer
        format: int64
        minimum: 0
        default: 0

@jmini jmini added this to the 3.0.0 milestone May 16, 2018
jmini added a commit to jmini/openapi-experiments that referenced this pull request May 18, 2018
Copy link
Member

@wing328 wing328 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performed some tests and the result is good.

Conflicts:
	modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java
	modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaModelEnumTest.java
@jmini jmini merged commit 82ee865 into OpenAPITools:master May 18, 2018
jmini added a commit to jmini/openapi-experiments that referenced this pull request May 18, 2018
@jmini jmini mentioned this pull request May 18, 2018
jimschubert added a commit to jimschubert/openapi-generator that referenced this pull request May 23, 2018
* master: (36 commits)
  jaxrs-cxf-cdi: fix outer enum (OpenAPITools#131)
  Move bash argv opt to end of ags line in scripts (OpenAPITools#124)
  Reduce CI logging (OpenAPITools#119)
  Download elm dependencies without prompting user. (OpenAPITools#118)
  [aspnetcore] Make the use of Swashbuckle optional (OpenAPITools#110)
  DefaultGenerator: fix NullPointerException (OpenAPITools#109)
  [Java] use html entities in javadoc of generated code (OpenAPITools#106)
  Update PULL_REQUEST_TEMPLATE.md
  [java] Enum in array of array (OpenAPITools#66)
  Rename datatype to dataType in CodegenProperty (OpenAPITools#69)
  update elm test to compile all elm files (OpenAPITools#95)
  Fix Petstore example for Elm (OpenAPITools#96)
  Update Docker documentation (OpenAPITools#97)
  CaseFormatLambda has been added, params for Rest-assured client has been refactored (OpenAPITools#91)
  Update integration.md
  [Clojure] Add util method to set the api-context globally (OpenAPITools#93)
  [JaxRS-Java] issue with implFolder on windows, and required fields generation for containers (OpenAPITools#88)
  Set parameters allowableValues dynamically (OpenAPITools#65)
  Meta: set version for "build-helper-maven-plugin" (OpenAPITools#89)
  Fix javadoc issues in "openapi-generator" module (OpenAPITools#84)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants