2016-07-28 161 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