Comments (2)
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.
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)
- Support Enums HOT 2
- Misleading error message on deserialization when subtype is unknown HOT 1
- Allow to disable behavior introduced by fix #100 in 1.8.0 - Stop StopLookupOnMatch HOT 2
- Incorrect link to license
- New Major to depend on newer Newtonsoft.Json HOT 3
- KnownSubTypeWithProperty it would be great to support aliases HOT 3
- Support (serializing) sub types with generic type parameters when using JsonSubtypesConverterBuilder HOT 7
- MaxDepth not copied over when creating internal JsonReader
- Serialize type discriminator when using attributes too HOT 6
- Use in API calls?
- Example code in readme fails, just completely wrong according to visual studio HOT 1
- JsonSubtypesConverterBuilder SetFallbackSubtype HOT 1
- wrong link in v2.0.1 release notes HOT 1
- JsonReaderException does not contain a constructor that takes 5 arguments HOT 4
- Persistent settings HOT 1
- Base Class Properties not being Deserialized HOT 2
- Outdated version of NewtsonSoft.Json used
- deserializing dates with jsonsubtypes does not match same result values as stock newtonsoft
- Error on deserialization "Could not find member ... on type" when MissingMemberHandling=error and there is no discriminator field on the dto HOT 1
- Support combination of independent discriminator types per inheritance level HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonsubtypes.