Skip to content

How to code generate nested array property using swagger-code-gen client #989

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

Closed
vijayrcse opened this issue Jul 20, 2015 · 15 comments
Closed

Comments

@vijayrcse
Copy link

I have a property which is List<List>

I tried to add the below in my .json file( using swaggercode gen cli - 2.1.2 )

"listOfListOfClassA": {
"type": "array",
"description": "description 1",
"items": {
"type": "array",
"description": "description 2",
"items": {
"$ref": "ClassA"
}
}
}

"ClassA": {
"id": "class_id",
"description": "Description for class A",
"properties": {
"propertyName1": {
"type": "string",
"description": "Property Name 1"
},
"propertyName2": {
"type": "string",
"description": "Property Name 1"
},
}
},

But I am getting the below exception
java.lang.NullPointerException
at java.util.regex.Matcher.getTextLength(Matcher.java:1283)
at java.util.regex.Matcher.reset(Matcher.java:309)
at java.util.regex.Matcher.(Matcher.java:229)
at java.util.regex.Pattern.matcher(Pattern.java:1093)
at io.swagger.codegen.DefaultCodegen.camelize(DefaultCodegen.java:1237)
at io.swagger.codegen.DefaultCodegen.camelize(DefaultCodegen.java:1212)
at io.swagger.codegen.languages.JavaClientCodegen.toModelName(JavaClientCodegen.java:179)
at io.swagger.codegen.languages.JavaClientCodegen.getSwaggerType(JavaClientCodegen.java:215)
at io.swagger.codegen.DefaultCodegen.getTypeDeclaration(DefaultCodegen.java:490)

Could you please advise ?

@vijayrcse vijayrcse changed the title How to code gene nested array How to code generate nested array property using swagger-code-gen client Jul 20, 2015
@wing328
Copy link
Contributor

wing328 commented Jul 21, 2015

@vijayrcse would be nice if you can share the full swagger spec definition with us (assuming it can be shared publicly) and also I wonder if you can do a test with the latest develop_2.0 branch to see if the exception still occurs.

@vijayrcse
Copy link
Author

wing328

This was the original .json file

{"apiVersion": "1.0", "swaggerVersion": "1.2", "basePath": "/", "resourcePath": "/service-v1", "produces": ["application/json"], "consumes": ["application/json"], "apis": [
    {
        "path": "/service-v1/evaluate",
        "operations": [
            {
                "method": "POST",
                "summary": "Summary",
                "notes": "notes",
                "type": "Response",
                "nickname": "evaluate",
                "produces": ["application/json"],
                "parameters": [
                    {
                        "name": "body",
                        "description": "Request description",
                        "required": true,
                        "type": "array",
                        "items": {
                            "$ref": "Request"
                        },
                        "paramType": "body",
                        "allowMultiple": false
                    }
                ],
                "responseMessages": [
                    {
                        "code": 200,
                        "message": "Successful evaluation",
                        "responseModel": "Response"
                    },
                    {
                        "code": 404,
                        "message": "service does not exist"
                    },
                    {
                        "code": 500,
                        "message": "Internal service error"
                    }
                ]
            }
        ]
    }
], "models": {
    "Attribute": {
        "id": "Attribute",
        "description": "Attribute representation",
        "properties": {
            "id": {
                "type": "integer",
                "format": "int32",
                "description": "Id"
            },
            "attributeId": {
                "type": "integer",
                "format": "int32",
                "description": "Attribute Id"
            },
            "attributeName": {
                "type": "string",
                "description": "Attribute Name"
            },
            "attributeValue": {
                "type": "string",
                "description": "Attribute Value"
            },
            "attributeValueCode": {
                "type": "string",
                "description": "Attribute Value Code"
            }
        }
    },
    "Response": {
        "id": "Response",
        "description": "response representation",
        "required": ["id", "exceptionType", "chains"],
        "properties": {
            "requestId": {
                "type": "string",
                "description": "Unique ID for mapping input and output"
            },
            "id": {
                "type": "integer",
                "format": "int32",
                "description": "ID"
            },
            "exceptionType": {
                "type": "string",
                "description": "Exception Type description"
            },
            "exceptionDetail": {
                "type": "string",
                "description": "Exception Details description"
            },
            "chains": {
                "$ref": "List[List[Descriptor]]",
                "description": "Ordered collection of Descriptor"
            }
        }
    },
    "Request": {
        "id": "Request",
        "description": "request representation",
        "required": ["id"],
        "properties": {
            "requestId": {
                "type": "string",
                "description": "Unique ID for mapping input and output"
            },
            "id": {
                "type": "integer",
                "format": "int32",
                "description": "ID"
            },
            "allAttribute": {
                "type": "array",
                "description": "Attributes",
                "items": {
                    "$ref": "Attribute"
                }
            }
        }
    }
}}

This doesn't complie while i try to generate code using swaggercode gen cli - 2.1.2. I noticed that List Of List of decriptor is not generated properly( java List<List<Descriptor>> )

So i edited to below

{"apiVersion": "1.0", "swaggerVersion": "1.2", "basePath": "/", "resourcePath": "/service-v1", "produces": ["application/json"], "consumes": ["application/json"], "apis": [
    {
        "path": "/service-v1/evaluate",
        "operations": [
            {
                "method": "POST",
                "summary": "Summary",
                "notes": "notes",
                "type": "Response",
                "nickname": "evaluate",
                "produces": ["application/json"],
                "parameters": [
                    {
                        "name": "body",
                        "description": "Request description",
                        "required": true,
                        "type": "array",
                        "items": {
                            "$ref": "Request"
                        },
                        "paramType": "body",
                        "allowMultiple": false
                    }
                ],
                "responseMessages": [
                    {
                        "code": 200,
                        "message": "Successful evaluation",
                        "responseModel": "Response"
                    },
                    {
                        "code": 404,
                        "message": "service does not exist"
                    },
                    {
                        "code": 500,
                        "message": "Internal service error"
                    }
                ]
            }
        ]
    }
], "models": {
    "Attribute": {
        "id": "Attribute",
        "description": "Attribute representation",
        "properties": {
            "id": {
                "type": "integer",
                "format": "int32",
                "description": "Id"
            },
            "attributeId": {
                "type": "integer",
                "format": "int32",
                "description": "Attribute Id"
            },
            "attributeName": {
                "type": "string",
                "description": "Attribute Name"
            },
            "attributeValue": {
                "type": "string",
                "description": "Attribute Value"
            },
            "attributeValueCode": {
                "type": "string",
                "description": "Attribute Value Code"
            }
        }
    },
    "Descriptor": {
    "id": "class_id",
    "description": "Description for Descriptor",
    "properties": {
    "propertyName1": {
    "type": "string",
    "description": "Property Name 1"
    },
    "propertyName2": {
    "type": "string",
    "description": "Property Name 1"
    }
    }
    },
    "Response": {
        "id": "Response",
        "description": "response representation",
        "required": ["id", "exceptionType", "chains"],
        "properties": {
            "requestId": {
                "type": "string",
                "description": "Unique ID for mapping input and output"
            },
            "id": {
                "type": "integer",
                "format": "int32",
                "description": "ID"
            },
            "exceptionType": {
                "type": "string",
                "description": "Exception Type description"
            },
            "exceptionDetail": {
                "type": "string",
                "description": "Exception Details description"
            },
            "chains": {
                "type": "array",
                "description": "description 1",
                "items": {
                "type": "array",
                "description": "description 2",
                "items": {
                "$ref": "Descriptor"
                }
                }
            }
        }
    },
    "Request": {
        "id": "Request",
        "description": "request representation",
        "required": ["id"],
        "properties": {
            "requestId": {
                "type": "string",
                "description": "Unique ID for mapping input and output"
            },
            "id": {
                "type": "integer",
                "format": "int32",
                "description": "ID"
            },
            "allAttribute": {
                "type": "array",
                "description": "Attributes",
                "items": {
                    "$ref": "Attribute"
                }
            }
        }
    }
}}
Now i get the below exception
java.lang.NullPointerException
    at java.util.regex.Matcher.getTextLength(Matcher.java:1283)
    at java.util.regex.Matcher.reset(Matcher.java:309)
    at java.util.regex.Matcher.<init>(Matcher.java:229)
    at java.util.regex.Pattern.matcher(Pattern.java:1093)
    at io.swagger.codegen.DefaultCodegen.camelize(DefaultCodegen.java:1237)
    at io.swagger.codegen.DefaultCodegen.camelize(DefaultCodegen.java:1212)
    at io.swagger.codegen.languages.JavaClientCodegen.toModelName(JavaClientCodegen.java:179)
    at io.swagger.codegen.languages.JavaClientCodegen.getSwaggerType(JavaClientCodegen.java:215)
    at io.swagger.codegen.DefaultCodegen.getTypeDeclaration(DefaultCodegen.java:490)
    at io.swagger.codegen.languages.JavaClientCodegen.getTypeDeclaration(JavaClientCodegen.java:200)
    at io.swagger.codegen.languages.JavaClientCodegen.getTypeDeclaration(JavaClientCodegen.java:193)
    at io.swagger.codegen.languages.JavaClientCodegen.getTypeDeclaration(JavaClientCodegen.java:193)
    at io.swagger.codegen.DefaultCodegen.fromProperty(DefaultCodegen.java:606)
    at io.swagger.codegen.DefaultCodegen.addVars(DefaultCodegen.java:1191)
    at io.swagger.codegen.DefaultCodegen.fromModel(DefaultCodegen.java:540)
    at io.swagger.codegen.DefaultGenerator.processModels(DefaultGenerator.java:451

is this becoz of swagger spec 1.2 being used to generate .json file ?

I am using swagger code gen 2.1.2 at client side but service is owned by different team where they provide .json file to us.

Please help how to proceed .

@wing328
Copy link
Contributor

wing328 commented Jul 28, 2015

Swagger spec 1.2 does not support nested array. Would you consider converting the spec to the latest Swagger spec 2.0, which supports nested array ?

@vijayrcse
Copy link
Author

Yes. I am following this to move to swagger spec 2.0 please let me know if this is correct
https://github.com/swagger-api/swagger-core/wiki/1.3--1.5-Migration

@vijayrcse
Copy link
Author

I have converted to 2.0 spec but i am getting below code generated at client side which doesn't compile

private List<List<Descriptor>> chains = new ArrayList<List>() ;

how should we get past this issue ?

generated json snippet

"chains": {
                "type": "array",
                "description": "Ordered descriptor chains",
                "items": {
                    "type": "array",

                    "items": {
                        "$ref": "#/definitions/Descriptor"
                    }
                }
            }

@fehguy
Copy link
Contributor

fehguy commented Jul 29, 2015

This is simply not supported in codegen right now. Is there another way you can describe the data other than [[{ /* descriptor */ }]]?

@vijayrcse
Copy link
Author

if this is not supported the only option is change the response not to contain

List<List<Descriptor>>

if spec 2.0 supports nested array , would like to know why code gen is not supporting that

other option for us to change as follows

List<DescriptorList>
class DescriptorList
{
    List<Descriptor> descriptor;
}

but this is going to change more things at service side
any thoughts if this can be supported ?

@xhh
Copy link
Contributor

xhh commented Jul 30, 2015

Hi @vijayrcse, were you using the latest code from the develop_2.0 branch when you tried it?
I just tried it and it works for me, generating code of model properties like this:

private List<List<Descriptor>> chains = new ArrayList<List<Descriptor>>();

@vijayrcse
Copy link
Author

could you please tell me what is the certified version that needs to be used ? I am using the below version

<dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-codegen-cli</artifactId>
            <version>2.1.2</version>
        </dependency>

@wing328
Copy link
Contributor

wing328 commented Jul 30, 2015

develop_2.0 branch is the latest version which contains bug fixes and enhancements. Please give it a try as it might have addressed the issue you reported.

@vijayrcse
Copy link
Author

I cannot build using develop_2.0 branch. It is prohibited and i can only use a certified maven release.
Do you have certified maven release version that contains the fix ?

@wing328
Copy link
Contributor

wing328 commented Jul 30, 2015

If that's the case, I'm afraid you've to wait for the next major release (no date has been set).

Maybe you can ask for an exception ?

@vijayrcse
Copy link
Author

nope not possible. is it possible if you can give something like 2.1.3 ?

@wing328
Copy link
Contributor

wing328 commented Aug 25, 2015

@vijayrcse 2.1.3 released: https://github.com/swagger-api/swagger-codegen/releases

please give it a try.

@wing328
Copy link
Contributor

wing328 commented Sep 9, 2015

closing this as there's no further update.

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

No branches or pull requests

4 participants