JSON模式中带有相同名称和不同类型的anyOf字段到POJO + Jackson。

huangapple go评论113阅读模式
英文:

Json schema with anyOf field with same name and different types to POJO + Jackson

问题

  1. 如果有一个包含AnyOf部分的组件A的架构,其中包含两个条目。它们之间的区别在于,在一种情况下,子组件C是数组,在另一种情况下,C是对象,但它们具有相同的名称B。如果我为此在Java中有两个对象,是否可能使用Jackson处理它呢?
  2. 我在思考是否可以使用带有某些注解的接口,然后Jackson将确定正确的对象...

"A": {
"type": "object",
"anyOf": [
{
"properties": {
"B": {
"type": "array",
"items": {
"type": "object",
"$ref": "#/components/schemas/C"
}
}
},
"additionalProperties": false
},
{
"properties": {

  1. "B": {
  2. "type": "object",
  3. "$ref": "#/components/schemas/C"
  4. }
  5. },
  6. "additionalProperties": false
  7. }
  8. ]
  9. }
  1. 假设我在Java中有这样的代码:

public class AAnyOf1 {

@JsonProperty("B")
private List b = new ArrayList<>();

...

}

public class AAnyOf2 {

@JsonProperty("B")
private C b = null;

...

}

  1. <details>
  2. <summary>英文:</summary>
  3. If have a schema with component A that contains AnyOf section with two items. The difference between them is that in one case child component C is array and in another one C is object but they have the same name B. Is it possible to handle it with jackson if I have two java objects for that?
  4. I&#39;m thinking if I can use interface with some annotations and jackson will determine the correct object...

"A": {
"type": "object",
"anyOf": [
{
"properties": {
"B": {
"type": "array",
"items": {
"type": "object",
"$ref": "#/components/schemas/C"
}
}
},
"additionalProperties": false
},
{
"properties": {

  1. &quot;B&quot;: {
  2. &quot;type&quot;: &quot;object&quot;,
  3. &quot;$ref&quot;: &quot;#/components/schemas/C&quot;
  4. }
  5. },
  6. &quot;additionalProperties&quot;: false
  7. }
  8. ]
  9. }
  1. Lets suppose I have this in java

public class AAnyOf1 {

@JsonProperty("B")
private List<C> b = new ArrayList<>();

...

}

public class AAnyOf2 {

@JsonProperty("B")
private C b = null;

...

}

  1. </details>
  2. # 答案1
  3. **得分**: 1
  4. 这是一种非常流行的模式,用于在响应中发送一个`JSON对象`,而不是带有一个`JSON对象`的`JSON数组`。因此,替代方式如下:

{
"b": {
"id": 1
}
}

  1. `API`响应如下:

{
"b": {
"id": 1
}
}

  1. `Jackson`已经处理了这种用例。您需要启用[ACCEPT_SINGLE_VALUE_AS_ARRAY][1]特性,并且只需要一个版本的`POJO`
  2. ```java
  3. class AAnyOf {
  4. @JsonProperty("B")
  5. private List<C> b;
  6. ...
  7. }
英文:

This is very popular pattern to send in response a JSON Object instead of JSON Array with one JSON Object. So, instead of:

  1. {
  2. &quot;b&quot;: [
  3. {
  4. &quot;id&quot;: 1
  5. }
  6. ]
  7. }

API response looks like:

  1. {
  2. &quot;b&quot;: {
  3. &quot;id&quot;: 1
  4. }
  5. }

Jackson already handle this use case. You need to enable ACCEPT_SINGLE_VALUE_AS_ARRAY feature and you need only one version of POJO:

  1. class AAnyOf {
  2. @JsonProperty(&quot;B&quot;)
  3. private List&lt;C&gt; b;
  4. ...
  5. }

huangapple
  • 本文由 发表于 2020年9月22日 23:13:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64012629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定