如何将JSON数组设置为定义类型?

时间:2016-09-28 19:49:13

标签: json jsonschema

如何设置数组中的对象类型以引用json模式文件的定义部分中的另一个已定义对象?我试过这样做:

"definitions": {
    "ObjectA": {
        "title": "ObjectA",
        "type": "object",
        "properties": {
            "description": {
                "type": "string"
            },
            "status": {
                "type": "string"
            },
        },
        "required": [
            "description",
            "status"
        ]
    },
    "ObjectB": {
        "title": "ObjectB",
        "type": "object",
        "properties": {
            "objectalist": {
                "type": "array",
                "items": {
                    "type": {
                        "$ref": "#/definitions/ObjectA"
                    }
                }
            }
        },
        "required": [
            "objectalist"
        ]
    }
}

并且json编辑器似乎认为它很好。此代码段是Swagger API定义的一部分,当我通过codegen工具运行它时,我收到此错误:

[main] ERROR io.swagger.codegen.DefaultCodegen - No Type defined for Property null
Exception in thread "main" java.lang.RuntimeException: Could not process model 'ObjectB'.Please make sure that your schema is correct!
        at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:297)
        at io.swagger.codegen.cmd.Generate.run(Generate.java:223)
        at io.swagger.codegen.SwaggerCodegen.main(SwaggerCodegen.java:36)
Caused by: java.lang.NullPointerException
        at io.swagger.codegen.languages.AbstractJavaCodegen.toModelName(AbstractJavaCodegen.java:400)
        at io.swagger.codegen.languages.AbstractJavaCodegen.getSwaggerType(AbstractJavaCodegen.java:577)
        at io.swagger.codegen.DefaultCodegen.getTypeDeclaration(DefaultCodegen.java:1119)
        at io.swagger.codegen.languages.AbstractJavaCodegen.getTypeDeclaration(AbstractJavaCodegen.java:427)
        at io.swagger.codegen.languages.AbstractJavaCodegen.toDefaultValue(AbstractJavaCodegen.java:440)
        at io.swagger.codegen.DefaultCodegen.fromProperty(DefaultCodegen.java:1359)
        at io.swagger.codegen.DefaultCodegen.addVars(DefaultCodegen.java:2738)
        at io.swagger.codegen.DefaultCodegen.addVars(DefaultCodegen.java:2709)
        at io.swagger.codegen.DefaultCodegen.addVars(DefaultCodegen.java:2695)
        at io.swagger.codegen.DefaultCodegen.fromModel(DefaultCodegen.java:1284)
        at io.swagger.codegen.languages.AbstractJavaCodegen.fromModel(AbstractJavaCodegen.java:601)
        at io.swagger.codegen.DefaultGenerator.processModels(DefaultGenerator.java:875)
        at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:290)
        ... 2 more

如果我将ObjectB更改为:

    "ObjectB": {
        "title": "ObjectB",
        "type": "object",
        "properties": {
            "objectalist": {
                "type": "array",
                "items": {
                    "type": "string"
                }
            }
        },
        "required": [
            "objectalist"
        ]
    }

codegen有效。有没有办法将数组中的对象类型设置为定义的类型?

1 个答案:

答案 0 :(得分:0)

在另一篇关于定义自定义JSON类型的文章中找到答案。答案是删除封闭的suites: SuitesFile 。所以,它看起来像这样:

"type":