Swagger: Remove class properties from parameter example value - json

I am using Swagger to document a REST API.
I am having a class like this:
public class Source{
private String url;
private String category;
private String label;
...
}
I am currently using #ApiImplicitParam to set the dataType to Source.class, but I am having multiple POST requests that get a JSON as a body parameter with, lets say, a single variable of those, for example:
{"label": "labelA"}
Because of the dataType set previously, the example value displayed by the Swagger UI is a whole Source.class, something like this:
{
"url": "string",
"category": "string",
"label": "string",
...
}
Could I somehow chop the example value displayed by the Swagger UI for every single request of those? I mean that the getSourceFromUrl() request should get a JSON that contains only a url field, and the example should display exactly this and not the full Source.class JSON.
Thank you all in advance!
UPDATE
I am using JAX-RS. Please, ask me for more input if needed.

If you are using springfox-swagger2 , there is an annotation #ApiModelProperty that does this.
Example:
#ApiModelProperty(required = false, hidden = true)
private String label;

For the time being, you cannot do such a thing. You have to create a different class for each case.
See in swagger-core's github: https://github.com/swagger-api/swagger-core/issues/1863#issuecomment-237339565

Related

Array to string conversion in EasyAdmin 3 (Symfony 5)

I'm trying to display a json array on the EasyAdmin detail page. I read here Is there a way to represent a JSON field in EasyAdmin 3? that you can use ArrayField in EasyAdmin 3 to display a json array, which would make sense to me as that is the only field in EasyAdmin that could display it so I added it like this:
public function configureFields(string $pageName): iterable
{
return [
ArrayField::new('status', $this->translator->trans('form.label.status'))->onlyOnDetail(),
];
}
But it gives me this error:
An exception has been thrown during the rendering of a template ("Notice: Array to string conversion"). Do I need to add something else to it or does it not work at all?
change "status" to a multiplicity "statuses"
Worked for me with changing "print" to "prints"
I found a workaround that resolved the issue in my situation where I wanted just to show JSON on details page
So in your entity add a get function as an unmapped field as indicated in the official documentaiton
https://symfony.com/bundles/EasyAdminBundle/current/fields.html#unmapped-fields
for example
public function getJsonField() {
return json_encode($this->yourEntityJsonAttribute);
}
then in configureFields function in your CrudController add your field like this
TextAreaField::new('jsonField')->onlyOnDetail()
You can also read the json attribute and generate an HTML string in the getJsonField function then in configure field just add renderAsHtml in th field like this
TextAreaField::new('jsonField')->onlyOnDetail()->renderAsHtml()
Wish this suits your case too, Good luck

Suppress empty List of Objects from Json output

I have a Data class which I populate manually from a XML returned from a trading Partner. Most of time the returned Data will only have a few of the fields in my class populated, the others I set to Nothing(null) and then use the
Dim settings As New JsonSerializerSettings
settings.NullValueHandling = NullValueHandling.Ignore
to suppress any field with no data. All that works great but I am wondering how I can do the same or similar for List of Objects in my class. Right now if there is no data for one of these I still get an an output like this which is an empty json Array.
{
"Txnum": "APO100000007R",
"Dtsent": "20180625105938",
"Txtyp": "A",
"Location": [],
"Terminationdata": [],
"Responsestatus": {
"prespc": "FTRAVQ059",
"prespd": "LV1 IS REQUIRED "
}
}
So I am wondering if and how I could avoid this short of checking json string before I return it and strip the "Location": [], for example.
This is the code that worked for me. Just as a Side Node the ShouldSerialize"PropertyName:() is case sensitive so ShouldSerializeLocation() works while ShouldSerializelocation() does not. In Vb code normally is not case sensitive so just watch for that to save your self some headache.
<XmlElement([Namespace]:=SynchronossNS)>
Public Property Location() As List(Of location)
Get
Return _location
End Get
Set
_location = Value
End Set
End Property
Public Function ShouldSerializeLocation() As Boolean
Return _location.Count > 0
End Function

obtain values to json file

I am making an application in vb net, and read JSON format files.
I know how, but I have a file that has a different format, you can see here:
{
"objects": {
"realms/lang/de_DE.lang": {
"hash": "729b2c09d5c588787b23127eeda2730f9c039194",
"size": 7784
},
"realms/lang/cy_GB.lang": {
"hash": "7b52463b2df4685d2d82c5d257fd5ec79843d618",
"size": 7688
},
"minecraft/sounds/mob/blaze/breathe4.ogg": {
"hash": "78d544a240d627005aaef6033fd646eafc66fe7a",
"size": 22054
},
"minecraft/sounds/dig/sand4.ogg": {
"hash": "37afa06f97d58767a1cd1382386db878be1532dd",
"size": 5491
}
}
}
It is different because all text has objects, no string, so I can not read it defined classes.
I just need the values of "hash", then add them to a textbox.
I hope you can help me.
Sounds like #Plutonix has seen this before, so I'll go with the Minecraft thing. It seems like objects is a Dictionary(Of String, MinecraftItem). A little tweak to the linked duplicate could be to create a class for the whole file, so you don't have to pull out the objects separately:
Public Class MinecraftData
Public Property Objects As Dictionary(Of String, MinecraftItem)
End Class
Public Class MinecraftItem
Public Property Hash As String
Public Property Size As Long
End Class
You can then parse the whole thing using Json.NET:
Dim data = JsonConvert.DeserializeObject(Of MinecraftData)(json)
If you are certain that you will only ever need the hashes, you can actually use a regex to get all the hashes from this file. Yes, this is slightly evil, because regex is normally not a suitable tool for dealing with structured data. But if you don't care about the structure...
Dim hashExtractor = new System.Text.RegularExpressions.Regex("[0-9a-f]{40}")
Dim matches = hashExtractor.Matches(myFileContents).Cast(of System.Text.RegularExpressions.Match)
Dim hashes = matches.Select(Function(m) m.Value).ToList()

Is this json badly constructed?

The following json text is the result of a call to eBay's search API.
Q: Is this json badly constructed? (It's technically, correct - so that's not what I'm referring to)
By that I mean, notice how -every value- is inside an array? instead of just being a "key" : "value" ?
eg.
"ack": [
"Success"
],
or
"version": [
"1.12.0"
],
etc..
Now, before you answer "well, maybe each key has multiple result values" .. I'm pretty sure most of them can't.
(this json schema is really making my life a pita. yes, it's easy to make each POCO property a List<string> but it's the point of it all!)
References: Here's the office eBay API documention for this endpoint
After reading the documentation, I understand eBay's approach, but I find it poor in terms of client side deserialisation. For example, the benefit of an array value for the ack property is that the value could also contain a warning, e.g.:
{
"ack": [
"Success",
"Warning"
]
}
However, a list of strings is not ideal for client side processing (e.g. in C#, bool hasWarning = pocoList.Contains("Warning"); doesn't strike me as completely foolproof). I'd rather have a response such as:
{
"ack": {
"result": "Success",
"warning": null
}
}
Then with my deserialised POCO, given that the value of warning is still a string, I could write this:
[DataContract]
public class Ack
{
[DataMember(Name="result")]
public string Result { get; set; }
[DataMember(Name="warning")]
public string Warning { get; set; }
[IgnoreDataMember]
public bool HasWarning
{
get { return !string.IsNullOrEmpty(Warning); }
}
}
This would allow me to replace my previous LINQ query with bool hasWarning = ack.HasWarning;.
There are definitely places where use of arrays is completely unnecessary. The docs describe the version property as "the release version that eBay used to process the request", thus I would return this as a single string. The array would only make sense if it was a versions property (e.g. for identifying all versions of the backend that support a specific request).
I've definitely seen worse JSON responses, but there are places where the APIs should ideally be returning a JSON object or a single value.
I normally use this tool for see if a JSON is good constructed:
http://json.parser.online.fr/
In your case, is not correct. You should store the data by Keys and Values.

Create dyanamic class to accept dynamic JSON

I have a web api endpoint that receives JSON and serializes it into objects. Very basic and common stuff. However, I have a requirement to accept custom user defined fields. For example, a developer may want to add a custom field for "account #" and pass that along via the API. I'm stuck how I could define a field on my class if I don't know the name. I need to support unlimited fields so I cannot simply create a field for custom1, custom2, custom2, etc.
I would think that my JSON could look something like this... where custom_label_xxx is identifies the field label:
...
"custom_fields": {
"custom_label_90": 49,
"custom_label_83": [ 28, 29, 30 ],
"custom_label_89": "2012/05/21"
},
...
How in the world can I setup a dynamic class to accept this dynamic JSON?
I have Googled forever and cannot find any examples using custom fields.
Your post method can accept dynamic as a param:
public HttpResponseMessage Post(dynamic obj)
This will accept every json that you send.
Saykor's reply is probably correct. However, I already have tons of business objects to work with and I only wanted the custom fields to be dynamic. Also, I wanted the Web API Help pages to be generated with sample JSON. So making the method dynamic would not work well for me.
What I finally did was probably obvious to some. I created a property on the class that was a dictionary. When I run the Help Pages it generates sample JSON that looks exactly what I needed. I have not tested this yet, but I assume it will serialize the JSON into a dictionary.
Here is the property in VB, but it could easily use dynamic in C# as well.
Private _customfields As Dictionary(Of String, String)
<DataMember(EmitDefaultValue:=False, IsRequired:=False, Order:=11)> _
Public Property customfields() As Dictionary(Of String, String)
Get
Return Me._customfields
End Get
Set(ByVal value As Dictionary(Of String, String))
Me._customfields = value
End Set
End Property
This resulted in the following JSON:
"customfields": {
"sample string 1": "sample string 2",
"sample string 3": "sample string 4",
"sample string 5": "sample string 6"
}