I'm trying to fetch a model schema for an item in Swagger. I'd like to do this through an http request from a different machine from the one hosting Swagger.
I can fetch the Swagger API-docs as json from:
domain.com/swagger/v2/api-docs.json
The response contains:
{
"swagger": "2.0",
...
paths: {
"/endpoint": {
"get": {
...
"responses": {
"200":{
"description":"OK",
"schema": {
"type":"array",
"items": {
"$ref":"#/definitions/Item"
}
}
}
}
}
}
}
}
Is there any way to fetch the "/definitions/Item" model schema?
I'd like to do an http GET on something like:
domain.com/swagger/v2/api-docs/definitions/Item.json
I'm using Swagger version 2.0.
Thanks
It's at the bottom of the same document. Eg: if you go to the live demo of the swagger editor (http://editor.swagger.io/#/edit) and scroll to the bottom you'll see the object definitions that are referenced in the endpoint definitions.
Related
I am exposing my API using OpenAPI 3.0. One of the models that I created was extended from org.springframework.hateoas.RepresentationModel. The model can be seen under the /components/schemas/ correctly with all the properties I created. However, the inherited property
private final List<Link> links;
are created with error
Links: {
title: "Links",
type: "object",
properties: {
links: {
type: "array",
items: {
$ref: "Error-ModelName{namespace='org.springframework.hateoas', name='Link'}"
}
}
}
}
Not sure how would I be able to fix this as this third-party lib. Please advise.
My API exposes some endpoints with DateTime? and others with DateTimeOffset?.
I want to generate an API Client using OpenApi-Generator, that will create client code for each endpoint, respecting the differences between the types.
OpenApi-Generator offers the option to useDateTimeOffset=true, which will generate the client using DateTimeOffset everywhere regardless of whether the API was exposing DateTime or DateTimeOffset. As such, I don't want to use that.
So to resolve the requirement without that, I'm attempting to create a Filter in which I will specify that in the case of DateTimeOffset? the generator should use the type DateTimeOffset and make it nullable.
This is the Filter:
public class FixDateTimeOffsetTypeSchemaFilter : ISchemaFilter
{
public void Apply(OpenApiSchema model, SchemaFilterContext context)
{
if (context.Type == typeof(DateTimeOffset?))
{
model.Format = "date-time-offset-nullable";
model.Type = "DateTimeOffset";
model.Nullable = true;
}
}
}
and this is an example of the output from that:
"/Api/MyApiEndpoint": {
"get": {
"tags": [
"General"
],
"operationId": "General_MyApiEndpoint",
"parameters": [
{
"name": "startDatetime",
"in": "query",
"schema": {
"type": "DateTimeOffset",
"format": "date-time-offset-nullable",
"nullable": true
}
},
When I run this and generate the client from that JSON, the the DateTimeOffset objects are never nullable. It doesn't seem to respect that instruction.
What do I need to change to make it work, so that when I specify DateTimeOffset should be nullable, it appears as nullable in the code?
It looks like this is a known problem:
https://github.com/OpenAPITools/openapi-generator/pull/3530
And here is the pull request that solves it: https://github.com/OpenAPITools/openapi-generator/pull/3530/commits/89fd5d70e9f15a5be9ffe26c2beddc07770043c0
Also check this link: https://jane.readthedocs.io/en/latest/tips/nullable.html
I generated swagger for micronaut using the instructions provided in https://micronaut-projects.github.io/micronaut-openapi/latest/guide/index.html
So I have a controller method like:
#Consumes("application/vnd.api+json")
#Produces("application/vnd.api+json")
#Post("/{id}/users")
#RequestBody
public HttpResponse addAndAssignTarget(#PathVariable("id") Long projectId, #Body #Parameter() JsonNode user) {
I am not using a POJO for adding users for another reason which is out of context for this question. Thus, the generated swagger ui shows {} as example for request body. I would like to change this to something like. How can I do this?
{
"data" : {
"type": "projects",
"attributes": {
"name": "some-name1",
"description": "some-description",
"partner_company": "some-compnay"
}
}
}
It looks much better if you use POJO. However, you can add an example as string, but the problem with that if you have any nested objects the quotation marks won't look nice (will be escaped "):
#Consumes("application/vnd.api+json")
#Produces("application/vnd.api+json")
#Post("/{id}/users")
#RequestBody
public HttpResponse addAndAssignTarget(#PathVariable("id") Long projectId,
#Body #RequestBody(content = #Content(schema = #Schema(example = "[0, 2, 3]"))) JsonNode user){
}
Also it is not a Paramater, but a #Body and #RequestBody for annotation purposes.
Output will be:
"[0, 2, 3]"
I have a subsite where I created a contentType. I want to a add columnLink from the parent site using:
POST https://graph.microsoft.com/v1.0/sites/{site-id}/contentTypes/{contentType-id}/columnLinks
Adding current site columns works as expected, however when adding columnLink from parent site, fails with such response:
{
"error": {
"code": "itemNotFound",
"message": "The referenced column does not exist",
"innerError": {
"request-id": "...,
"date": "2018-07-31T11:05:34"
}
}
}
The body that was sent:
{
"name": "Detail"
}
The endpoint works correctly for both id and name in request body. (For current site columnLinks)
According to your questions, I suppose you want to add a columnLink from parent site.
Based on my test, If we want to add a column to a contentType, we can get the column first by this API:
https://graph.microsoft.com/v1.0/sites/{site-id}/columns. This step is to ensure that the column exists. The part of response will be like this:
{
"name": "{column Name}",
"id": "{column Id}"
}
Then we add site columnLink to a contentType, we can use this column Name like this:
POST https://graph.microsoft.com/v1.0/sites/{site-id}/contentTypes/{contentType-id}/columnLinks
The body like this:
{
"name":"{column Name}"
}
or
{
"id": "{column Id}"
}
I need to pass a dynamic JSON object to my Web API controller so that I can process it depending on what type it is. I have tried using the JSON.NET example that can be seen here but when I use Fiddler, I can see that the passed in JObect is always null.
This is an exert from the example pasted into Fiddler:
POST http://localhost:9185/api/Auto/PostSavePage/ HTTP/1.1
User-Agent: Fiddler
Content-type: application/json
Host: localhost
Content-Length: 88
{AlbumName: "Dirty Deeds",Songs:[ { SongName: "Problem Child"},{ SongName:
"Squealer"}]}
Ans here is my very simple Web API controller method:
[HttpPost]
public JObject PostSavePage(JObject jObject)
{
dynamic testObject = jObject;
// other stuff here
}
I am new to this and I have a couple of questions around this area:
Am I doing something wrong in this particular example?
Arguably, more importantly, is there a better way to pass in a dynamic JSON object (from an JavaScript AJAX post)?
As per Perception's comment your JSON doesn't look valid. Run it through JSONLint and you get:
Parse error on line 1:
{ AlbumName: "Dirty De
-----^
Expecting 'STRING', '}'
Change it to have " around the field names:
{
"AlbumName": "Dirty Deeds",
"Songs": [
{
"SongName": "Problem Child"
},
{
"SongName": "Squealer"
}
]
}
Also have you tried swapping out your JObject for either a JToken or a Dynamic object (e.g. here)?
[HttpPost]
public JObject PostSavePage(JToken testObject)
{
// other stuff here
}
OR
[HttpPost]
public JObject PostSavePage(dynamic testObject)
{
// other stuff here
}
Thanks to everyone who helped here. Unfortunately, I never got to the bottom of what was wrong.
I ported the project over piece by piece to a new project and it works fine.
For info, I have a RouteConfig class, which is quite simple at the moment:
public class RouteConfig
{
private static string ControllerAction = "ApiControllerAction";
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: ControllerAction,
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
My call into the API now uses JSON.Stringify:
$.ajax("http://localhost:54997/api/values/PostSavePage/", {
data: JSON.stringify(jObject),
contentType: 'application/json',
type: 'POST'
});
The original API action works.
Note, that I'm only playing with this at the moment so the code is not the best but I thought it may be useful in basic form in case someone else has a similar issue.