Django rest swagger and json definition - json

I'm pretty new to Django and I'm currently working on a project where Django-rest-framework is used and the documentation is created with Swagger trough the django-rest-swagger package.
I'm trying to find (or generate) a swagger.json definition file, any idea on where it can be found with this package? I would like to use it to generate client side code.
Thanks

The swagger schema definition can be generated with https://github.com/signalfx/fetch-swagger-schema regardless the api implementation.

You can append ?format=openapi to the end of your docs URL and it will return the raw JSON text instead of the UI.
see here

Related

Render HAL formatted links in openapi3 json with SpringDoc

I'm running a Spring Boot REST application with Spring HATEOAS support and generating OpenAPIv3 docs with the Springdoc Maven plugin. However when I call my REST endpoints I get links the in HAL formatted JSON ("_links"). But the generated OpenAPIv3 documentation is giving me a different format for the links ("links").
How can I get the generated OpenAPIv3 docs to match the HAL formatted links?
The only resource I've found is this link: https://github.com/springdoc/springdoc-openapi/issues/446
However, the solution given there involves using spring-data-rest which I am not using (do I need to?)
I've also tried adding #EnableHyperMediaSupport which says it configures the JSON rendering, but that had no effect on the OpenAPIv3 docs.
The answer was simple enough, I needed to pull in the springdoc-openapi-hateoas dependency (https://springdoc.org/#spring-hateoas-support). After pulling this in the JSON documentation was generated correctly with no additional configuration (I did not need #EnableHypermediaSupport or spring-data-rest).
However, if you are using the Swagger UI be aware that it will automatically generate bogus 'additionalProperty' links as an example for the resource schema. This is only in the Swagger UI, if you look at the generated openapiv3 json the structure is correct (cf: https://github.com/springdoc/springdoc-openapi/issues/237). To remedy this you can provide your own example of the resource schema.

How does a .json file work in a API call?

I've been doing research in order to write an API for a school project, and when referencing the API documentation of YouTube and Twitter, I see API URLs like this
https://api.twitter.com/1.1/account/settings.json
My understanding was that you execute a method on the backend which will return information to the caller, but I thought those files had to be of extension type .py or .java or whatever language you're using, and JSON was just the return type. I've been unable to find any information on how a .json file works in this example. Is there code in settings.json that is being executed?
JSON is just a format of your data, that you can then use, for example in JavaScript.
It is back-end language independent. By this I mean, that front-end of the application does not care who produced .json file.
Maybe it was Java application, maybe it was Python, or PHP application it does not matter. It can be also static file, with fixed content which just has json format.
After you receive such thing in front-end, you can do with it whatever you want. From your perspective it will be probably some kind of nested array.
In example you provided, I get:
{"errors":[{"code":215,"message":"Bad Authentication data."}]}
And it is fine, it's just data you get. It is JSON format - that is true. But you don't care that path has .json in the URL, it could have any extension, what is important is what's inside.
That is a beauty of JSON. You can prepare for yourself static file with mocked data in JSON format, and use it while developing front-end of the application. When you wish, you can have back-end application which will return real data for your app.
You can also see here, how to return json file from PHP:
Returning JSON from a PHP Script
Or here to see how to do it in Python (Django Framework):
Creating a JSON response using Django and Python

WSo2 API Manager 1.8.0 - JSON parsing issue

I am new to wso2 API Manager, trying to set it up expose my plain HTTP POST back end calls as a REST API. I am sure this is not a new pattern that I am trying to achieve here. The requirement is to convert the JSON data coming in (has array structures) into the HTTP URL query string parameters.
After some research through the documentation and other posts on this forum, decided to go with the script mediator that would parse the JSON data and convert it to a string that can be appended to the endpoint URL. Some how I am not able to achieve this.
I have followed the following post that seems to be very straight forward. As the original poster suggested, I am also not able to use getPayloadJSON() method. Even have trouble using the work around suggested there due to JSON.parse() not working.
Link
Also, this approach of editing the service bus configuration from source view does not sound like the correct option. Is there another elegant solution to achieve this? Thanks for the help in advance.
I was able to get both methods working by using an external script instead of the inline java script. Thanks

Moustache template for Swagger codegen static documentation - responseMessages

I am trying to use swagger-codegen to generate static-docs.
The docs are generated based on Moustache templates that are included in the project.
When I run it with the sample JSON from wordnik Swagger api-docs, it generates everything perfectly (every api has it's own .file like Pet.html, User.html), but when I try to run it with similar JSON of mine, it generates only 1 operations file containing all the methods of my REST api.
Wordnik JSON reponse can be found at worndik JSON api
Mine API response looks like this:
{"apiVersion":"1.0","swaggerVersion":"1.2","apis":[{"path":"/default/countries","description":"Operations about countries"},{"path":"/default/gateways","description":"Operations on payment gateways"},{"path":"/default/location","description":"Operations about locations"},{"path":"/default/mccs","description":"Operations about MCCs"},{"path":"/default/merchants","description":"Operations about merchants"},{"path":"/default/partners","description":"Operations about partners"},{"path":"/default/payments","description":"Operations about payments"},{"path":"/default/resources","description":"Operations about resources"},{"path":"/default/terminals","description":"Operations about terminals"},{"path":"/default/terminalsubsetdefaultresourceset","description":"Operations about terminalSubsetDefaultResourceSet"},{"path":"/default/users","description":"Operations about users"}],"info":{"title":"my API","description":"","termsOfServiceUrl":"","contact":"","license":"","licenseUrl":""}}
Also, I would like to extract ReponseMessage Codes in every operation that has them in JSON. I tried to add
{{#ResponseMessages}}
<h3 class="responseMessages">{{message}}</h3>
{{/ResponseMessages}}
to operations.model, but It doesn't work (not with myApi, nor with Wordnik) (I have similar JSON like this:JSON with responseCodes
You should be able to use the following (at least it works for me), based on the current version of Codegen.scala
{{#errorList}}
{{code}} {{reason}} {{responseModel}}
{{/errorList}}

HTML::Template Perl

IIs there a package similar to HTml::Template in perl which takes a JSON object and maps it to a HTML template file? I am building a web application using HTML::Template and will be receiving JSOn from a web services API, things will be made simple if I can templatize this JSOn to HTML instead of doing it the exact way HTML::Template requires.
HTML::Template just takes a data structure consisting of strings, hashes and arrays. JSON maps directly onto that.
$template->param(myData => JSON::Any->new->decode($json_string));
HTML::Template is a rather 'simple' templating engine - I am using quotes because its simplicity let's you do whatever you need in a view part from the Model View Controller architecture.
However, you can not execute arbitrary perl code inside a HTML::Template.
Also, due to the fact that in JSON you could have very complex data structures, I doubt that there are any suitable ways of using JSON in a straight way in your templates.
The only solution I see as possible is for you to use a Perl script that will parse the JSON, create some 'objects' and pass them to your templates. You already have that perl script - is the one that instantiate your HTML::Template object.
ok, a bit late, but:
HTML::Template always wants a hash of arrays of hashes and so on.
and you cannot navigate through the parameter stash.
If you want to do this, you might try out HTML::Template::Compiled which allows you to do this.
<tmpl_var some_hash.key.another_key[23] >
or with alternative delimiters:
[%= some_hash.key.another_key[23] %]
but note the documented differences of the module to HTML::Template.
So you decode your JSON string to a data structure and pass it to the template and then you can access all values somewhere deep in the structure.