-
Notifications
You must be signed in to change notification settings - Fork 368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Swagger schema reports all unit properties and not UnitValue ones #1256
Comments
I fixed it (to some extents) using an ISchemaFilter: In Program.cs:
The ISchemaFilter is defined in this way:
It works: now I have the right Example Values and Schema. |
Thanks for sharing. I don't have any knowledge on this specific problem with Swagger, but I would recommend you to consider creating your own data transfer DTO types for representing quantities. Then you are in full control of its serialization, and you can remove all the cruft that exists in these types, since you typically just need Value and Unit properties. Just create a simple mapping function to/from The DTO could be as simple as: public record QuantityDto(double Value, string QuantityType, string Unit);
// Example:
QuantityDto dto = new(Value: 5.0, QuantityType: "Length", Unit: "Centimeter"); Then you don't need any special serializers injected here and there to make things work. Also, if we incur a breaking change in our JSON serializer, which we do really try to avoid, then your API gets a breaking change. |
I realize it was not obvious how to accomplish this, so I created some helper methods New methods added: |
Nuget is out shortly, with the new methods. |
Describe the bug
When using UnitsNetIQuantityJsonConverter (and hence Newtonsoft.JSon serializer), Swagger generates the schema of all units (e.g. Length) as they are as C# classes, reporting all their properties (value, unit, feetInches, quantityInfo, dimensions and all related subobjects).
Bug UnitsNetIQuantityJsonConverter convert values such that they are (de)serialized as ValueUnit, i.e. having value and unit properties only.
To Reproduce
Steps to reproduce the behavior:
Use this solution: WebTest.zip
It exposes a single method as a Rest API (/Api), returning a MyDto object, having Location = 12.345 Km.
Try calling /Api.
The result will correctly be:
But MyDto will have Example Values:
I truncated it a lot, since it is veeeeeery long (more than 360 lines).
Please note that this is not coherent with the JSON actually returned by the API. This is the bug!
And MyDto will have schema:
Expected behavior
Example Values should be:
Schema should be:
Additional context
The problem is very easy to spot and seems easy to fix: we are using a converter, but Swagger has no idea of it. We just need to inform Swagger about the substitution of all IQuantityValue with ValueUnit (ExtendedValueUnit, in some cases).
The text was updated successfully, but these errors were encountered: