From f5513ee79aa683d16dec8e029bdb280a3da4dd0f Mon Sep 17 00:00:00 2001 From: Joost van de Wijgerd Date: Wed, 2 Sep 2020 11:02:36 +0200 Subject: [PATCH] implemented the propery methods that take property name and type as inputs. This is needed to support custom (de)serializers that are using these methods. This can only be used with the DefaultTagGenerator so an IllegalStateException will be thrown if this gets mixed with properties annotated with @JsonProperty.index --- .../AnnotationBasedTagGenerator.java | 5 +++ .../schemagen/MessageElementVisitor.java | 37 ++++++++++++++++--- .../protobuf/schemagen/TagGenerator.java | 2 + 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/schemagen/AnnotationBasedTagGenerator.java b/protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/schemagen/AnnotationBasedTagGenerator.java index 827733f70..f26c6351e 100644 --- a/protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/schemagen/AnnotationBasedTagGenerator.java +++ b/protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/schemagen/AnnotationBasedTagGenerator.java @@ -13,4 +13,9 @@ public int nextTag(BeanProperty writer) { throw new IllegalStateException("No index metadata found for " + writer.getFullName() + " (usually annotated with @JsonProperty.index): either annotate all properties of type " + writer.getWrapperName().getSimpleName() + " with indexes or none at all"); } + + @Override + public int nextTag() { + throw new IllegalStateException("Index metadata not found, (usually annotated with @JsonProperty.index): either annotate all properties with indexes or none at all"); + } } diff --git a/protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/schemagen/MessageElementVisitor.java b/protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/schemagen/MessageElementVisitor.java index 27f6567a8..69ad76a5c 100644 --- a/protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/schemagen/MessageElementVisitor.java +++ b/protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/schemagen/MessageElementVisitor.java @@ -50,8 +50,8 @@ public void property(BeanProperty writer) throws JsonMappingException { } @Override - public void property(String name, JsonFormatVisitable handler, JavaType propertyTypeHint) { - throw new UnsupportedOperationException(); + public void property(String name, JsonFormatVisitable handler, JavaType propertyTypeHint) throws JsonMappingException { + _builder.addField(buildFieldElement(name, propertyTypeHint, Label.REQUIRED)); } @Override @@ -60,8 +60,8 @@ public void optionalProperty(BeanProperty writer) throws JsonMappingException { } @Override - public void optionalProperty(String name, JsonFormatVisitable handler, JavaType propertyTypeHint) { - throw new UnsupportedOperationException(); + public void optionalProperty(String name, JsonFormatVisitable handler, JavaType propertyTypeHint) throws JsonMappingException { + _builder.addField(buildFieldElement(name, propertyTypeHint, Label.OPTIONAL)); } protected FieldElement buildFieldElement(BeanProperty writer, Label label) throws JsonMappingException @@ -88,6 +88,33 @@ protected FieldElement buildFieldElement(BeanProperty writer, Label label) throw return fBuilder.build(); } + protected FieldElement buildFieldElement(String name, JavaType type, Label label) throws JsonMappingException + { + FieldElement.Builder fBuilder = FieldElement.builder(); + + fBuilder.name(name); + fBuilder.tag(nextTag()); + + if (type.isArrayType() || type.isCollectionLikeType()) { + if (ProtobufSchemaHelper.isBinaryType(type)) { + fBuilder.label(label); + fBuilder.type(ScalarType.BYTES); + } else { + fBuilder.label(Label.REPEATED); + fBuilder.type(getDataType(type.getContentType())); + } + } else { + fBuilder.label(label); + fBuilder.type(getDataType(type)); + } + return fBuilder.build(); + } + + protected int nextTag() { + getTagGenerator(null); + return _tagGenerator.nextTag(); + } + protected int nextTag(BeanProperty writer) { getTagGenerator(writer); return _tagGenerator.nextTag(writer); @@ -95,7 +122,7 @@ protected int nextTag(BeanProperty writer) { protected void getTagGenerator(BeanProperty writer) { if (_tagGenerator == null) { - if (ProtobufSchemaHelper.hasIndex(writer)) { + if (writer != null && ProtobufSchemaHelper.hasIndex(writer)) { _tagGenerator = new AnnotationBasedTagGenerator(); } else { _tagGenerator = new DefaultTagGenerator(); diff --git a/protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/schemagen/TagGenerator.java b/protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/schemagen/TagGenerator.java index 7d4b5776a..d8c275c0f 100644 --- a/protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/schemagen/TagGenerator.java +++ b/protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/schemagen/TagGenerator.java @@ -4,4 +4,6 @@ interface TagGenerator { int nextTag(BeanProperty writer); + + int nextTag(); }