2016-09-28 53 views
0

如何設置數組中對象的類型以引用json模式文件的定義部分中的另一個已定義對象?我試過這樣做:如何將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編輯器似乎認爲它很好。這個片段是一個揚鞭API定義的一部分,當我通過代碼生成工具來運行它,我得到這個錯誤:如果我改變對象B到

[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": { 
     "title": "ObjectB", 
     "type": "object", 
     "properties": { 
      "objectalist": { 
       "type": "array", 
       "items": { 
        "type": "string" 
       } 
      } 
     }, 
     "required": [ 
      "objectalist" 
     ] 
    } 

的代碼生成作品。有沒有辦法將數組中的對象類型設置爲定義的類型?

回答

0

在另一篇文章中找到了關於定義自定義JSON類型的答案。答案是刪除封閉的"type":。所以,看起來像這樣:

"ObjectB": { 
     "title": "ObjectB", 
     "type": "object", 
     "properties": { 
      "objectalist": { 
       "type": "array", 
       "items": { 
        "$ref": "#/definitions/ObjectA" 
       } 
      } 
     }, 
     "required": [ 
      "objectalist" 
     ] 
    } 
相關問題