Giter Club home page Giter Club logo

Comments (2)

manuc66 avatar manuc66 commented on May 18, 2024

Hi,

It seems the generaration is not correct according to the spec you provide and my understandig about OpenApi (see https://swagger.io/specification/#discriminatorObject) , I would expect somthing like this:

    [DataContract]
    [JsonConverter(typeof(JsonSubtypes), "shapeType")]
    [JsonSubtypes.KnownSubType(typeof(Shape2DReactangle), "2d-rectangle")]
    [JsonSubtypes.KnownSubType(typeof(Shape2DPolygon), "2d-polygon")]
    public partial class Shape :  IEquatable<Shape>, IValidatableObject
    {...

Given the fact that types are anotated with attributes you don't need to build a converter, there is already a converter associted to the type with this line:

[JsonConverter(typeof(JsonSubtypes), "shapeType")]

I don't see the full definition of Shape but with [JsonConverter(typeof(JsonSubtypes), "shapeType")] you need to have the property shapeType otherwise it will not be serialized.

If you want to register yourself the serializer you must remove the attribute JsonConverter and it will work with this:

using System;
using JsonSubTypes;
using Newtonsoft.Json;
					
public class Program
{
    //[JsonConverter(typeof(JsonSubtypes), "shapeType")]
    //[JsonSubtypes.KnownSubType(typeof(Shape2DReactangle), "2d-rectangle")]
    //[JsonSubtypes.KnownSubType(typeof(Shape2DPolygon), "2d-polygon")]
    public class Shape
    {
		string shapeId { get; set; }
	}
	
 
    public class Shape2DPolygon : Shape
    {
		
	}	

    public class Shape2DReactangle : Shape
    {
		public int area { get; set; }
	}
	
	
	public static void Main()
	{		
		
		var stg = new JsonSerializerSettings();
		stg.Converters.Add(JsonSubTypes.JsonSubtypesConverterBuilder
									.Of(typeof(Shape), "shapeType")
									.RegisterSubtype(typeof(Shape2DReactangle), "2d-rectangle")
									.RegisterSubtype(typeof(Shape2DPolygon), "2d-polygon")
									.SerializeDiscriminatorProperty() // ask to serialize the type property
									.Build());
		
		var serial = JsonConvert.SerializeObject(new Shape2DReactangle() {area = 5}, stg);
		Console.WriteLine(serial);	
		
	}
}

=> See running sample at https://dotnetfiddle.net/xwnDWg or https://dotnetfiddle.net/A1JpiC

from jsonsubtypes.

andreiStoicescu avatar andreiStoicescu commented on May 18, 2024

Thanks for the response. It seems that the openapi generator does not generate the field for discriminator and if i add the field as a property then the conversion works in c# but i doesn't work in java

from jsonsubtypes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.