Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert C# object to IOpenApiAny [closed]

I wish to set OpenApiSchema.Example. Which is an IOpenApiAny.

Can i convert/cast a simple c# object to an existing IOpenApiAny, ex. Microsoft.OpenApi.Any.OpenApiObject? or i need to implement the interface on all my objects

Example

var schma = new Microsoft.OpenApi.Models.OpenApiSchema
{
    Example = new MyObject();//doesn't work, how can i cast it?
};

Background: I am trying to create a Swashbuckle filter which adds examples to request and response parameters and schemas. I am using the upcoming Swashbuckle 5.0 (in beta) which is using OpenAPI.NET, so I am trying to create my filter using that.

like image 962
N T Avatar asked Dec 06 '19 01:12

N T


People also ask

How do we convert a Celsius?

The formula for converting Fahrenheit to Celsius is C = 5/9(F-32). Fahrenheit and Celsius are the same at -40°. At ordinary temperatures, Fahrenheit is a larger number than Celsius. For example, body temperature is 98.6 °F or 37 °C.

What is type conversions in C?

In C programming, we can convert the value of one data type ( int, float , double , etc.) to another. This process is known as type conversion.


1 Answers

Your class MyObject needs to implement IOpenApiAny, as follows:

public class MyObject : IOpenApiAny
{
    // implement everything here that IOpenApiAny defines:

    public AnyType AnyType { get; }

    public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
    {
        // implementation
    }
}

If you have lots of such classes, and you can come up with a common implementation for all of them, then you can use a single abstract base class and let all the concrete classes inherit from that:

public abstract class BaseObject : IOpenApiAny
{
    // implement everything defined by IOpenApiAny (see above)
}

public class MyClass1 : BaseObject
{
    // additional "MyClass1" methods/props
}

public class MyClass2 : BaseObject
{
    // additional "MyClass2" methods/props
}
like image 120
Peter B Avatar answered Oct 02 '22 00:10

Peter B