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

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

Closed
vidyas78 opened this issue Mar 9, 2018 · 27 comments
Closed

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

vidyas78 opened this issue Mar 9, 2018 · 27 comments

Comments

@vidyas78
Copy link

vidyas78 commented Mar 9, 2018

@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

@webron
Copy link
Contributor

webron commented Mar 9, 2018

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

@jmini
Copy link
Contributor

jmini commented Mar 12, 2018

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.

@vidyas78
Copy link
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`]

@vidyas78
Copy link
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;

@jmini
Copy link
Contributor

jmini commented Mar 13, 2018

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.

@vidyas78
Copy link
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//.

@vidyas78
Copy link
Author

Team

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

Thanks

@jmini
Copy link
Contributor

jmini commented Mar 16, 2018

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

@vidyas78
Copy link
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>>.

@jmini
Copy link
Contributor

jmini commented Mar 17, 2018

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.

@vidyas78
Copy link
Author

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

@jmini
Copy link
Contributor

jmini commented Mar 19, 2018

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
Copy link
Contributor

jmini commented Mar 19, 2018

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

jmini added a commit to jmini/swagger-codegen-generators that referenced this issue Mar 19, 2018
@vidyas78
Copy link
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

@jmini
Copy link
Contributor

jmini commented Mar 21, 2018

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

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

@msjoshi
Copy link

msjoshi commented Mar 27, 2018

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.

@jmini
Copy link
Contributor

jmini commented Mar 27, 2018

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.

@msjoshi
Copy link

msjoshi commented Mar 28, 2018

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

@jmini
Copy link
Contributor

jmini commented Mar 28, 2018

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
Copy link
Contributor

jmini commented Mar 28, 2018

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.

@jmini
Copy link
Contributor

jmini commented Mar 28, 2018

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

@msjoshi
Copy link

msjoshi commented Mar 28, 2018

@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

@jmini
Copy link
Contributor

jmini commented Mar 28, 2018

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.

@vidyas78
Copy link
Author

vidyas78 commented Apr 3, 2018

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

@vidyas78
Copy link
Author

vidyas78 commented Apr 3, 2018

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

@jmini
Copy link
Contributor

jmini commented Apr 3, 2018

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

@webron webron added this to the v3.0.0 milestone Apr 3, 2018
@webron
Copy link
Contributor

webron commented Apr 3, 2018

Thanks @jmini.

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

No branches or pull requests

4 participants