2016-07-28 189 views
1

我们正试图从SOAP转移到REST,并且我们偶然发现了这个问题 此处派对可以是Individual或Organization类型。JSON和对象继承

样品个XML

<customer> 
    <details> 
    <party xsi:type="Individual"> 
     <externalID>ABC123</externalID> 
     <firstname>John</firstname> 
     <lastname>Smith</lastname> 
    </party> 
    </details> 
</customer> 

<customer> 
    <details> 
    <party xsi:type="Organization"> 
     <externalID>APPLE</externalID> 
     <organizationName>Apple Inc</organizationName> 
     <listingName>APPLE</listingName> 
    </party> 
    </details> 
</customer> 

然而,当我们移动到相同的JSON表示,我们会​​遇到继承信息丢失

JSON样品

{ 
    "customer": { 
    "details": { 
     "party": { 
     "externalID": "ABC123", 
     "firstname": "John", 
     "lastname": "Smith" 
     } 
    } 
    } 
} 

{ 
    "customer": { 
    "details": { 
     "party": { 
     "externalID": "APPLE", 
     "organizationName": "Apple Inc", 
     "listingName": "APPLE" 
     } 
    } 
    } 
} 

所以,这个问题的时候,我们使用像Gson这样的库将JSON转换回Java对象,我们放弃了Individual或Organization的定义。

虽然其中一种解决方法是构建附加服务以检索返回具体类型(个人或组织)的“详细信息”,但是还有其他方法可以在JSON中处理此问题吗?

+0

这似乎是一个重复的问题。我认为这是你正在寻找的东西:http://stackoverflow.com/questions/33627344/equal-of-xsitype-in​​-json-schema –

回答

0

我找到了IBM提交的资源,觉得它可能有些帮助。它在下面链接。

On pg。 27它们转换:

<year xsi:type="xs:positiveInteger">1989</year> 

要:

{ "xsi:type" : "xs:positiveInteger", value : 1989 } 

因此,我认为你的代码转换为:

{ 
    "customer": { 
    "details": { 
     "party": { 
     "xsi:type": "Individual", 
     "externalID": "ABC123", 
     "firstname": "John", 
     "lastname": "Smith" 
     } 
    } 
    } 
} 

{ 
    "customer": { 
    "details": { 
     "party": { 
     "xsi:type": "Organization", 
     "externalID": "APPLE", 
     "organizationName": "Apple Inc", 
     "listingName": "APPLE" 
     } 
    } 
    } 
} 

资源以供参考:https://www.w3.org/2011/10/integration-workshop/s/ExperienceswithJSONandXMLTransformations.v08.pdf

1

它已经可以使用关键字(如oneOf,allOf,anyOf)组合模式并从JSON sch获取已验证的有效内容ema v1.0。

https://spacetelescope.github.io/understanding-json-schema/reference/combining.html

然而,组合物已经由关键字鉴别上的OpenAPI(前扬鞭)并入勉强支撑多态性增强。在OpenAPI 3.0中,通过添加oneOf关键字,此支持得到了增强。

您的继承可以使用oneOf(用于选择其中一个子项)和allOf(用于组合父项和子项)的组合进行建模。

paths: 
    /customers: 
    post: 
    requestBody: 
    content: 
     application/json: 
     schema: 
      oneOf: 
      - $ref: '#/components/schemas/Individual 
      - $ref: '#/components/schemas/Organization 
      discriminator: 
      propertyName: customer_type 
    responses: 
    '201': 
     description: Created 
components: 
    schemas: 
    Customer: 
     type: object 
     required: 
     - customer_type 
     - externalID 
     properties: 
     customer_type: 
      type: string 
     externalID: 
      type: integer 
     discriminator: 
     property_name: customer_type 
    Individual: 
     allOf: 
     - $ref: "#/components/schemas/Customer" 
     - type: object 
     - properties: 
      firtName 
      type: string 
      lastName 
      type: string 
    Organisation: 
     allOf: 
     - $ref: "#/components/schemas/Customer" 
     - type: object 
     - properties: 
      organisationName 
      type: string 
      listingName 
      type: string 

https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#schemaComposition

+0

确实,美洲国家组织允许这一点。但是,不支持在Swagger UI中显示该功能([link](https:// github。com/swagger-api/swagger-ui/issues/2438)),如果你不能向任何人展示它,我认为这个功能的使用有限。 – emft