JSON和对象继承 [英] JSON and object inheritance
问题描述
我们正尝试从SOAP过渡到REST,我们偶然发现了这个问题 在这里,party可以是个人或组织类型.
We are trying to move from SOAP to REST and we stumbled across this issue Here party can be of type Individual or Organization.
示例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表示时,会遇到继承信息丢失的问题
However when we move to JSON representation of the same, we encounter the problem where the inheritance information is lost
JSON示例
{
"customer": {
"details": {
"party": {
"externalID": "ABC123",
"firstname": "John",
"lastname": "Smith"
}
}
}
}
{
"customer": {
"details": {
"party": {
"externalID": "APPLE",
"organizationName": "Apple Inc",
"listingName": "APPLE"
}
}
}
}
因此,当我们使用诸如Gson之类的库将JSON转换回Java对象时,我们会松开个人"或组织"的定义.
So when we convert the JSON back to Java object using libraries like Gson, we loose the definition of Individual or Organization.
虽然解决方法之一是构建其他服务来检索返回具体类型(个人或组织)的详细信息", 还有其他方法可以在JSON中处理吗?
While one of the workaround is to build additional services to retrieve the "details" returning the concrete types (Individual or Organization), is there any other approach to handle this in JSON?
推荐答案
自JSON模式v1.0起,可以使用诸如oneOf,allOf,anyOf之类的关键字组合模式并获得经过验证的有效负载.
It's been possible to combine schemas using keywords such as oneOf, allOf, anyOf and get the payload validated since JSON schema v1.0.
https://spacetelescope.github.io/understanding-json -schema/reference/combining.html
但是,OpenAPI(以前的Swagger)中加入了关键字 discriminator 来增强了组合,从而几乎不支持多态性.在OpenAPI 3.0中,通过添加 oneOf 关键字增强了此支持.
However, composition has been enhanced by the keyword discriminator incorporated on OpenAPI (former Swagger) to barely support polymorphism. In OpenAPI 3.0, this support has been enhanced by the addition of oneOf keyword.
可以使用oneOf(用于选择子项之一)和allOf(用于组合父项和子项)的组合来建模您的继承.
Your inheritance could be modeled using a combination of oneOf (for choosing one of the children) and allOf (for combining parent and child).
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
2020年2月18日修改:
为了使用杰克逊生成Java代码,
18/02/2020
For generating Java code using Jackson, OpenAPITools has seemingly been fixed by the PR #5120
这篇关于JSON和对象继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!