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

JAVA: generating generic list/array property of nested model fails

Open RunCMD159 opened this issue 10 years ago • 16 comments

Description

I have a Model A which has a property with type: array that references another Model B which also has a property with type: array and references another Model. When generating the Model A i get a generic List with Model B but in Model B the referenced Model is not generated and the List $ref string as generic type.

Swagger-codegen version

gradle plugin: 'org.detoeuf.swagger-codegen' version '1.6.1'

Swagger declaration file content or url

ModelA.yaml: definitions: ModelA: type: object properties: propertyA: type: array items: { $ref: './ModelB.yaml#/definitions/ModelB' }

ModelB.yaml: definitions: ModelB: type: object properties: propertyA1: type: array items: { $ref: './ModelC.yaml#/definitions/ModelC' }

ModelC.yaml: definitions: ModelC: type: object properties: property1: {type: string} property2: {type: number} property3: {type: string}

==generated Models==

ModelA: `public class ModelA {

private List '<'ModelB'>' propertyA = new ArrayList'<'ModelB'>'();`

this is right.

ModelB: public class ModelB {

private List '<'ModelCYamldefinitionsModelC'>' propertyA1 = new ArrayList '<'ModelCYamldefinitionsModelC'>'();

this not. And ModelC is not even generated.

Command line used for generation

gradlew swagger

Steps to reproduce

generate the models for a path.

Related issues
Suggest a Fix

RunCMD159 avatar Aug 01 '16 13:08 RunCMD159

I think the issue is related to https://github.com/swagger-api/swagger-codegen/issues/3305

wing328 avatar Aug 01 '16 15:08 wing328

As a workaround, defining the models within the same file (spec) should work without issue.

wing328 avatar Aug 01 '16 15:08 wing328

I am having this issue as well. But my spec is huge, so I can't specify in the same file.

jeff9finger avatar Aug 01 '16 16:08 jeff9finger

I have this issue as well and it is critical to our use of swagger. Our spec is huge, so we must split our spec into multiple files.

jeff9finger avatar Aug 01 '16 16:08 jeff9finger

Another possible workaround (as mentioned in #3305) is to revert Swagger Parser version to 1.0.19

wing328 avatar Aug 01 '16 16:08 wing328

The issue is also present with *.json definitions, and downgrading Swagger Parser version didn`t help =( The only difference in our case is the type being "object" in nested references, not "array"

dennisTS avatar Aug 08 '16 11:08 dennisTS

Can you guys please pull the latest master to give it a try (as a newer stable version of Swagger parser has been released)?

wing328 avatar Aug 09 '16 03:08 wing328

Hi, wing328! Seems like the problem is still there, but it looks differently (or might be another one, not sure). Let us have models A1 and A2, each of A-models referencing model B. Model B, in turn, references model C (../Path/to/C.json#/definitions/C):

  1. When processing model A1, ExternalRefPRocessor processes model B and adds it to cache. Then it processes properties of model B, stripping them from external reference part (before #/definitions/...), and putting them into cache. Now model B references model C as an INTERNAL model (#/definitions/C)
  2. When processing model A2, ExternalRefPRocessor gets model B from cache and continues to resolving properties one more time. It now sees references to C as INTERNAL, trying to resolve it by adding path to B (../Path/to/B.json#/definitions/C), and, of course, doesn`t find it there, failing with RuntimeException: Could not find definitions/C in contents of ../Path/to/B.json

dennisTS avatar Aug 09 '16 11:08 dennisTS

@dennisTs thanks for performing more tests to confirm. Do you mind sharing with me the spec so that I can more easily reproduce the issue?

If the spec cannot be shared publicly, please send it to [email protected] (email address stated in my github profile)

wing328 avatar Aug 09 '16 11:08 wing328

@wing328 Nested_references_API.zip - here is a stub definition to reproduce the issue

dennisTS avatar Aug 09 '16 13:08 dennisTS

@wing328 just checked - it is a separate issue =( the one described by FlorianSchmuck still exists

dennisTS avatar Aug 10 '16 10:08 dennisTS

I tried with the latest version swagger-codegen-cli-3.0.0-rc0 and still see the missing imports for the nested arrays. Is there any solution released yet?

vidyas78 avatar Mar 09 '18 02:03 vidyas78

@vidyas78 can you please share the command and spec you use to get the error?

HugoMario avatar Mar 09 '18 10:03 HugoMario

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'

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

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

Thanks

vidyas78 avatar Mar 09 '18 15:03 vidyas78

thanks @vidyas78 , since this is for 3.0.0, can you please file a new ticket with these info and tag me there? i'll address the issue.

HugoMario avatar Mar 09 '18 16:03 HugoMario

sure will do it. We are currently using 2.2.3 for my project and to overcome this issue, I was trying all the later versions until 3.0.0.

Do you know any specific version where this issue is fixed? Or any workaround?

vidyas78 avatar Mar 09 '18 20:03 vidyas78