swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

Nested array data type missing import and fails in build

Open vidyas78 opened this issue 8 years ago • 27 comments

@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

vidyas78 avatar Mar 09 '18 21:03 vidyas78

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

webron avatar Mar 09 '18 22:03 webron

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.

jmini avatar Mar 12 '18 10:03 jmini

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`]

vidyas78 avatar Mar 13 '18 01:03 vidyas78

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;

vidyas78 avatar Mar 13 '18 01:03 vidyas78

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.

jmini avatar Mar 13 '18 05:03 jmini

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

vidyas78 avatar Mar 13 '18 14:03 vidyas78

Team

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

Thanks

vidyas78 avatar Mar 15 '18 14:03 vidyas78

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

jmini avatar Mar 16 '18 05:03 jmini

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

vidyas78 avatar Mar 16 '18 14:03 vidyas78

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.

jmini avatar Mar 17 '18 15:03 jmini

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

vidyas78 avatar Mar 18 '18 23:03 vidyas78

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.

jmini avatar Mar 19 '18 05:03 jmini

I have propsed a pull request to solve your issue: https://github.com/swagger-api/swagger-codegen-generators/pull/44

jmini avatar Mar 19 '18 17:03 jmini

  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

vidyas78 avatar Mar 21 '18 20:03 vidyas78

You need to work until the 2 merge requests proposed here are reviewed and merged:

  • https://github.com/swagger-api/swagger-codegen/pull/7863
  • https://github.com/swagger-api/swagger-codegen-generators/pull/44

I hope this will be addressed soon.

Of course you can fetch my branches and build locally. Be aware that you need to add all maven modules in the same maven build. This pom might help you.

I am sorry but I did not keep the jars (I am already working on other issues).

jmini avatar Mar 21 '18 20:03 jmini

I have a similar issue #7918. Will your merge request work for version 2.3.0. We are not ready to use the latest 3.x.x version.

msjoshi avatar Mar 27 '18 20:03 msjoshi

This can probably be back-ported, but the repos are different, so it means the the work needs to be done twice. I have no interest to do it myself.


You can work and load v2 Spec with version 3.0.0.

jmini avatar Mar 27 '18 21:03 jmini

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

msjoshi avatar Mar 28 '18 15:03 msjoshi

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.

jmini avatar Mar 28 '18 15:03 jmini

A build of the v3 branch was triggered with commit a33c06d876cc3cd428c7c0002eadbe39c770385c. 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.

jmini avatar Mar 28 '18 15:03 jmini