Skip to content

Nested array data type missing import and fails in build #7812

Closed

Description

@vidyas78

@HugoMario

Description

When we define nested arrays (arrays containing arrays) in yaml file and later generate code , it fails to import the inner array type model. This causes build failure.

Swagger-codegen version

swagger-codegen-cli-3.0.0-rc0.jar

But I see this issue since 2.2.3 until 3.0.0?

Swagger declaration file content or url

api.yaml definition contains the "array of array" definition,

swagger: '2.0'
info:
title:
description:
version: "1.0.0"
host:
schemes:

http
https
basePath: /v1
produces:
application/json
paths:
/patients/{uuid}/forms:
get:
summary:
operationId:
description: |
tags:

  • forms
    parameters:
  • name: uuid
    in: path
    description: identifier uuid.
    required: true
    type: string
  • name: start
    in: query
    description: Start date-time for data.
    required: false
    type: string
    format: dateTime
  • name: end
    in: query
    description: End date-time for data.
    required: false
    type: string
    format: dateTime
    responses:
    200:
    description: An array of updates
    headers:
    Access-Control-Allow-Origin:
    type: string
    default: "*"
    schema:
    type: array
    items:
    type: array
    items:
    $ref: '#/definitions/WaveformsUpdate'

Once the files are generated , I see missing imports for WaveformsUpdate and mvn clean install fails due to that.

Command line used for generation

Command to generate: java -jar libs/swagger-codegen-cli-3.0.0-rc0.jar generate -l java -i api.yaml -o target

Steps to reproduce

Create a yaml as below and generate code out of it.

type: array
items:
type: array
items:
$ref: '#/definitions/'

Related issues/PRs

I have see similar bugs in previous versions.

Suggest a fix/enhancement

Nothing for now. Please suggest if any

Activity

  1. webron commented on Mar 9, 2018

    @webron
    Contributor

    @vidyas78 the yaml you pasted is completely flat and missing indentation, can't really be used that way.

  2. jmini commented on Mar 12, 2018

    @jmini
    Contributor

    As mentioned by @webron it is hard to follow what your input spec is.

    Please try the latest 3.0.0-SNAPSHOT version, because I made some changes with array in order to fix: #7592

    As far as I know, in Java ArraySchema are converted to List<?>. Maybe an array of array will be a List of List in the generated Java code.

  3. vidyas78 commented on Mar 13, 2018

    @vidyas78
    Author

    Please see the yaml content inserted as code this time. Hope the indentation is taken care now.
    Command used to generate code,
    java -jar libs/swagger-codegen-cli-2.2.3.jar generate -l java -i contracts\api.yaml -o target
    Observation: target\src\main\java\io\swagger\client\model had "WaveformsUpdate.java" whereas
    target\src\main\java\io\swagger\client\api\FormsApi.java had missing import for WaveformsUpdate.

    `swagger: '2.0'
    info:
    title: Data Platform API
    description: Platform API
    version: "1.0.0"
    host: api.com
    schemes:

    • http
    • https
      basePath: /v1
      produces:
    • application/json
      paths:
      /patients/{uuid}/waveforms:
      get:
      summary: Return all the data items
      operationId: getWaveforms
      description: |
      Return all the waveform data items
      tags:
      - forms
      parameters:
      - name: uuid
      in: path
      description: identifier uuid.
      required: true
      type: string
      - name: start
      in: query
      description: Start date-time for data.
      required: false
      type: string
      format: dateTime
      - name: end
      in: query
      description: End date-time for data.
      required: false
      type: string
      format: dateTime
      responses:
      200:
      description: An array of updates (one per every source)
      headers:
      Access-Control-Allow-Origin:
      type: string
      default: "*"
      schema:
      type: array
      items:
      type: array
      items:
      $ref: '#/definitions/WaveformsUpdate'
      definitions:
      WaveformsUpdate:
      type: object
      description: "An update for a collection of parameters"
      properties:
      entityID:
      description: "The internal id"
      type: string
      sourceID:
      description: "Identifier of source emitting the data"
      type: string
      watchID:
      description: "Unique id of the data stream"
      type: string
      dateTime:
      description: "Date/time of the sample"
      type: string
      format: [dateTime`]
  4. vidyas78 commented on Mar 13, 2018

    @vidyas78
    Author

    Tried the same with "swagger-codegen-cli-3.0.0-20180307.151249-37.jar" & still issue exist.
    Command used to generate code,
    java -jar libs/swagger-codegen-cli-3.0.0-20180307.151249-37.jar generate -l java -i contracts\api.yaml -o target

    If you replace the array of array definition to array of waveformsupdate you can see the swagger-codegen-cli inserts the below import in FormsApi.java

    import io.swagger.client.model.WaveformsUpdate;

  5. jmini commented on Mar 13, 2018

    @jmini
    Contributor

    Without indentation, it is to hard to follow. Please edit your messages.

    Have a look at the markdown syntax guide. For multiline code, you need to use 3 grave accent. As alternative, you can also save your spec example at http://gist.github.com/ and then add the link here.

  6. vidyas78 commented on Mar 13, 2018

    @vidyas78
    Author

    https://gist.github.com/vidyas78/737441244829907cd961ce2a986ec94c

    Please refer from above & let me know if this helps, the yaml was validated in https://editor.swagger.io//.

  7. vidyas78 commented on Mar 15, 2018

    @vidyas78
    Author

    Team

    Can you please acknowledge if the issue was reproducible at your end?

    Thanks

  8. jmini commented on Mar 16, 2018

    @jmini
    Contributor

    I am sorry but I am not able to follow.

    I have generated code using the java generator with the spec taken from your gist.
    I can compile the generated artefact with maven => BUILD SUCCESS

    When I look at /<output folder>/src/main/java/io/swagger/client/api/FormsApi.java, I see following import lines:

    package io.swagger.client.api;
    
    import io.swagger.client.ApiCallback;
    import io.swagger.client.ApiClient;
    import io.swagger.client.ApiException;
    import io.swagger.client.ApiResponse;
    import io.swagger.client.Configuration;
    import io.swagger.client.Pair;
    import io.swagger.client.ProgressRequestBody;
    import io.swagger.client.ProgressResponseBody;
    
    import com.google.gson.reflect.TypeToken;
    
    import java.io.IOException;
    
    import io.swagger.client.model.WaveformsUpdate;
    
    import java.lang.reflect.Type;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class FormsApi { /* ... */ }
    

    This includes the expected WaveformsUpdate import.


    Maybe I missed something from your bug description. You have described 2 cases. Maybe the one you posted as gist is the one working...

  9. vidyas78 commented on Mar 16, 2018

    @vidyas78
    Author

    Ooops you are right, I made the revision 2 in gist by updating as array[array]. Please generate the same way and you can see the missing import for <List<List>>.

  10. jmini commented on Mar 17, 2018

    @jmini
    Contributor

    I have tried your example with the latest version 3.0.0-SNAPSHOT and I did not manage to reproduce your issue.

    I have found an other issue: #7863
    With the fix proposed in the Pull Request, the method looks correct:

        public List<List<WaveformsUpdate>> getWaveforms(String uuid, String start, String end) throws ApiException {
            ApiResponse<List<List<WaveformsUpdate>>> resp = getWaveformsWithHttpInfo(uuid, start, end);
            return resp.getData();
        }
    

    The imports look also good regarding List... I have found an other problem with imports: I think that some imports for the model classes are missing, but this looks like other issues that are reported here (#7776 and #7836). I will try to have a look at this.

  11. vidyas78 commented on Mar 18, 2018

    @vidyas78
    Author

    Yes imports of the model classes are missing, Can you please share the swagger-codegen-cli version once its fixed? Thanks

  12. jmini commented on Mar 19, 2018

    @jmini
    Contributor

    Forget what I have told about the other issues. I think your problem is different and I think I have found a solution.

    About the swagger-codegen-cli version containing the fix: I am only a contributor to the project, submitting pull request. I can not influence when they are merged. Improving swagger-codegen is not my job.

  13. jmini commented on Mar 19, 2018

    @jmini
    Contributor

    I have propsed a pull request to solve your issue: swagger-api/swagger-codegen-generators#44

  14. vidyas78 commented on Mar 21, 2018

    @vidyas78
    Author
    1. How do we know if swagger-codegen-cli/3.0.0-SNAPSHOT/ is updated with the jar with this fix?
    2. Do you have a copy of locally built jar that I can test with to confirm the fix?

    Thanks

  15. 3 remaining items

  16. msjoshi commented on Mar 28, 2018

    @msjoshi

    @jmini Now that the PR is merged, please help me understand how do I generate the cli jar from source? My understanding is download the latest source code of swagger-codegen and compile.
    However, I could not understand how is swagger-codegen-generators is referenced in this project.

  17. jmini commented on Mar 28, 2018

    @jmini
    Contributor

    In v3, the swagger-codegen-generators jar now contains the fix.

    The next swagger-codegen build will produce a cli jar that contains the latest version of swagger-codegen-generators. You can also build it locally. Checkout this repo swagger-codegen branch 2.0 and run mvn clean install -U in the root folder.

    If you are using the maven plugin. I think that if you run your build with -U, using version 3.0.0-SNAPSHOT of the swagger-codegen-maven-plugin maven will also fetch io.swagger:swagger-codegen-generators:1.0.0-SNAPSHOT (this needs to be verified).

    @msjoshi: do not hesitate to ask if you need something.

  18. jmini commented on Mar 28, 2018

    @jmini
    Contributor

    A build of the v3 branch was triggered with commit a33c06d. This means that if you download cli jar version 3.0.0-SNAPSHOT from maven central, you should get a cli jar that includes the io.swagger:swagger-codegen-generators:1.0.0-SNAPSHOT jar that contains the fix.

  19. jmini commented on Mar 28, 2018

    @jmini
    Contributor

    @webron, @HugoMario can you please add java-client and java-server and issue labels. And 3.0.0 milestone to this issue?

  20. msjoshi commented on Mar 28, 2018

    @msjoshi

    @jmini Let me know if I did this correctly.
    I downloaded the source code from - https://github.com/swagger-api/swagger-codegen/tree/a33c06d876cc3cd428c7c0002eadbe39c770385c

    I verified in pom.xml for swagger-codegen project that it is referencing to 3.0.0-SNAPSHOT.

    <name>swagger-codegen-project</name>
    <version>3.0.0-SNAPSHOT</version>
    

    And pom for swagger-codegen-cli is referencing to correct version of swagger-codegen-generators-version.

    <properties>
            <swagger-codegen-generators-version>1.0.0-SNAPSHOT</swagger-codegen-generators-version>
        </properties>
    

    Then I ran mvn clean install -U.

    However, it did not resolve the nested array issue. When I try to compile the client, it would give an error.

    [ERROR] /Users/mjoshi/projects/papi/app/src/swagger/clients/papi-client-java/src/main/java/io/swagger/client/model/ActionRoleSearch.java:[148,45] cannot find symbol
    [ERROR]   symbol:   class OrderEnum
    [ERROR]   location: class io.swagger.client.model.ActionRoleSearch
    
  21. jmini commented on Mar 28, 2018

    @jmini
    Contributor

    The snapshot Builds are here:

    https://oss.sonatype.org/content/repositories/snapshots/io/swagger/swagger-codegen-cli/3.0.0-SNAPSHOT/

    Now the latest build is 3.0.0-20180328.154333-52.

    As explained in the README, you can download it with:

    wget https://oss.sonatype.org/content/repositories/snapshots/io/swagger/swagger-codegen-cli/3.0.0-SNAPSHOT/swagger-codegen-cli-3.0.0-20180328.154333-52.jar -O swagger-codegen-cli.jar
    

    (or manually click on "save link as..." and indicate swagger-codegen-cli.jar as name, save it somewhere on your computer. I have used a tmp folder)

    To test it (in your tmp folder):

    java -jar swagger-codegen-cli.jar langs
    

    Then to generate assuming that spec.yaml is your input spec (located near to the jar) and that you want the output to go in a output/ folder:

    java -jar swagger-codegen-cli.jar generate -l java -o output/ -i spec.yaml
    

    For me it works with this spec provided by @vidyas78 I could run mvn verify in the output/ folder. I get no error.

    Second verification: file output/src/main/java/io/swagger/client/api/FormsApi.java contains the expected import line: import io.swagger.client.model.WaveformsUpdate;


    @msjoshi: There is no need to continue the discussion here. #7918 might be a similar but other problem. Test your case with the swagger-codegen-cli-3.0.0-20180328.154333-52.jar jar, and if you still have a problem, explain it in #7918.

  22. vidyas78 commented on Apr 3, 2018

    @vidyas78
    Author

    Hi Jmini,

    I tried my spec with swagger-codegen-cli-3.0.0-20180328.065958-50.jar but import is still missing in the API. Was wondering how it works for you, am I using the right cli?

    thanks

  23. vidyas78 commented on Apr 3, 2018

    @vidyas78
    Author

    Sorry for missing the version, its working with swagger-codegen-cli-3.0.0-20180328.154333-52.jar. Thanks

  24. jmini commented on Apr 3, 2018

    @jmini
    Contributor

    @webron, @HugoMario can you please add java-client and java-server and issue labels and 3.0.0 milestone to this issue? Can you also close it?

  25. added this to the v3.0.0 milestone on Apr 3, 2018
  26. webron commented on Apr 3, 2018

    @webron
    Contributor

    Thanks @jmini.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions