@@ -146,6 +146,17 @@ class ProtobufSerializer(object):
146146 | ``auto.register.schemas`` | bool | previously associated with a particular subject. |
147147 | | | Defaults to True. |
148148 +-------------------------------------+----------+------------------------------------------------------+
149+ | | | Whether to use the latest subject version for |
150+ | ``use.latest.version`` | bool | serialization. |
151+ | | | WARNING: There is no check that the latest |
152+ | | | schema is backwards compatible with the object |
153+ | | | being serialized. |
154+ | | | Defaults to False. |
155+ +-------------------------------------+----------+------------------------------------------------------+
156+ | | | Whether to skip known types when resolving schema |
157+ | ``skip.known.types`` | bool | dependencies. |
158+ | | | Defaults to False. |
159+ +-------------------------------------+----------+------------------------------------------------------+
149160 | | | Callable(SerializationContext, str) -> str |
150161 | | | |
151162 | ``subject.name.strategy`` | callable | Instructs the ProtobufSerializer on how to construct |
@@ -194,12 +205,15 @@ class ProtobufSerializer(object):
194205 `Protobuf API reference <https://googleapis.dev/python/protobuf/latest/google/protobuf.html>`_
195206
196207 """ # noqa: E501
197- __slots__ = ['_auto_register' , '_registry' , '_known_subjects' ,
208+ __slots__ = ['_auto_register' , '_use_latest_version' , '_skip_known_types' ,
209+ '_registry' , '_known_subjects' ,
198210 '_msg_class' , '_msg_index' , '_schema' , '_schema_id' ,
199211 '_ref_reference_subject_func' , '_subject_name_func' ]
200212 # default configuration
201213 _default_conf = {
202214 'auto.register.schemas' : True ,
215+ 'use.latest.version' : False ,
216+ 'skip.known.types' : False ,
203217 'subject.name.strategy' : topic_subject_name_strategy ,
204218 'reference.subject.name.strategy' : reference_subject_name_strategy
205219 }
@@ -214,6 +228,16 @@ def __init__(self, msg_type, schema_registry_client, conf=None):
214228 if not isinstance (self ._auto_register , bool ):
215229 raise ValueError ("auto.register.schemas must be a boolean value" )
216230
231+ self ._use_latest_version = conf_copy .pop ('use.latest.version' )
232+ if not isinstance (self ._use_latest_version , bool ):
233+ raise ValueError ("use.latest.version must be a boolean value" )
234+ if self ._use_latest_version and self ._auto_register :
235+ raise ValueError ("cannot enable both use.latest.version and auto.register.schemas" )
236+
237+ self ._skip_known_types = conf_copy .pop ('skip.known.types' )
238+ if not isinstance (self ._skip_known_types , bool ):
239+ raise ValueError ("skip.known.types must be a boolean value" )
240+
217241 self ._subject_name_func = conf_copy .pop ('subject.name.strategy' )
218242 if not callable (self ._subject_name_func ):
219243 raise ValueError ("subject.name.strategy must be callable" )
@@ -266,6 +290,8 @@ def _resolve_dependencies(self, ctx, file_desc):
266290 """
267291 schema_refs = []
268292 for dep in file_desc .dependencies :
293+ if self ._skip_known_types and dep .name .startswith ("google/protobuf/" ):
294+ continue
269295 dep_refs = self ._resolve_dependencies (ctx , dep )
270296 subject = self ._ref_reference_subject_func (ctx , dep )
271297 schema = Schema (_schema_to_str (dep ),
@@ -313,15 +339,22 @@ def __call__(self, message_type, ctx):
313339 message_type .DESCRIPTOR .full_name )
314340
315341 if subject not in self ._known_subjects :
316- self ._schema .references = self ._resolve_dependencies (
317- ctx , message_type .DESCRIPTOR .file )
342+ if self ._use_latest_version :
343+ latest_schema = self ._registry .get_latest_version (subject )
344+ self ._schema_id = latest_schema .schema_id
318345
319- if self ._auto_register :
320- self ._schema_id = self ._registry .register_schema (subject ,
321- self ._schema )
322346 else :
323- self ._schema_id = self ._registry .lookup_schema (
324- subject , self ._schema ).schema_id
347+ self ._schema .references = self ._resolve_dependencies (
348+ ctx , message_type .DESCRIPTOR .file )
349+
350+ if self ._auto_register :
351+ self ._schema_id = self ._registry .register_schema (subject ,
352+ self ._schema )
353+ else :
354+ self ._schema_id = self ._registry .lookup_schema (
355+ subject , self ._schema ).schema_id
356+
357+ self ._known_subjects .add (subject )
325358
326359 with _ContextStringIO () as fo :
327360 # Write the magic byte and schema ID in network byte order
0 commit comments