Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3
Bindings
Bindings exists in their own package.
Writing is exceedingly simply for bindings. Simply reference the binding and write. There is no need to do anything special Say we have the following (Example for the Pulsar ServerBinding)
vardoc=newAsyncApiDocument();doc.Info=newAsyncApiInfo(){Description="test description",};doc.Servers.Add("production",newAsyncApiServer{Description="test description",Protocol="pulsar+ssl",Url="example.com",Bindings=newAsyncApiBindings<IServerBinding>(){newPulsarServerBinding(){Tenant="Staging",}},});varyaml=doc.SerializeAsYaml(AsyncApiVersion.AsyncApi2_0);Yields the following yaml output
asyncapi: '2.6.0'info:
description: test descriptionservers:
production:
url: example.comprotocol: pulsar+ssldescription: test descriptionbindings:
pulsar:
tenant: StagingSay we want to read the yaml from before
asyncapi: '2.6.0'info:
description: test descriptionservers:
production:
url: example.comprotocol: pulsar+ssldescription: test descriptionbindings:
pulsar:
tenant: StagingWe need to specifically add binding-support to the reader, through AsyncApiReaderSettings Note there are some nifty helpers in the BindingsCollection class, that helps add collections of bindings (or All), but individual bindings can also be added.
varsettings=newAsyncApiReaderSettings();settings.Bindings=BindingsCollection.Pulsar;// Pulsar Server and Channel bindingsvarreader=newAsyncApiStringReader(settings);vardeserialized=reader.Read(yaml,outvardiagnostic);The above will deserialize the binding correctly.
There are 4 types of bindings and thus 4 abstract classes that can be implemented;
ServerBinding<T>ChannelBinding<T>OperationBinding<T>MessageBinding<T>
T must be the same type as the class that implements it.
The steps to create a custom binding
- Reference the bindings package through NuGet.
- Create your binding (in your own codebase)
publicclassFooBinding:ChannelBinding<FooBinding>{publicstringFoo{get;set;}publicstringBar{get;set;}publicoverridestringBindingKey=>"foo";protectedoverrideFixedFieldMap<FooBinding>FixedFieldMap=>newFixedFieldMap<FooBinding>(){{"bindingVersion",(a,n)=>{a.BindingVersion=n.GetScalarValue();}},{"foo",(a,n)=>{a.Foo=n.GetScalarValue();}},{"bar",(a,n)=>{a.Bar=n.GetScalarValue();}},};publicoverridevoidSerializeProperties(IAsyncApiWriterwriter){writer.WriteStartObject();writer.WriteRequiredProperty("foo",this.Foo);writer.WriteRequiredProperty("bar",this.Bar);writer.WriteOptionalProperty(AsyncApiConstants.BindingVersion,this.BindingVersion);writer.WriteExtensions(this.Extensions);writer.WriteEndObject();}}- Follow the same steps as in Reading or Writing above. Note. For Writing; you must explicitly add the custom bindings to the ReaderSettings.