How to show input and output exemple with nelmio-api-bundle and php8.1 - api

I'm currently trying to make an API doc page thanks to nelmio-api-bundle. I only have one route which is a POST route. I'm receiving a JSON in the body of the request and I'm using the Serializer from symfony to deserialize it in a DTO. I'm also using a DTO for the response (which contains the status code, a bool set to true or false, and a message). Now I'm trying to use these DTO (for input and output) to build the API documentation with nelmio-api-bundle but how to make it ? I'm using PHP8.1 attributes to make it, for response it almost works (except that the response is shows as an array) but I don't know how to make it for the inputs.
Here is my current code:
#[Route('/user', methods: ['POST'])]
#[OA\Parameter(
name: 'user',
description: 'The user information in JSON',
in: 'query',
required: true
)]
#[OA\Response(
response: 200,
description: 'Returns the success response',
content: new OA\JsonContent(
type: 'array',
items: new OA\Items(ref: new Model(type: SuccessResponseDTO::class))
)
)]
public function registerUser(Request $request, LoggerInterface $logger): JsonResponse
{
//the code to register the user
}
And here is the result:
Do someone know how to make it ?

Related

oas3 kotlin codegenerator: using multipart/form-data and free-form JSON object in the same request

I'm using OAS3 codegenerator to create Kotlin server code for a request containing both a file-upload, and a free-form json object that can contain arbitrary values. According to the swagger documentation, I should be able to create a free-form object that using type: object and additionalProperties : true.
However if I edit the upload photos endpoint in modules/openapi-generator/src/test/resources/3_0/petstore.yaml in the oas3 codegen samples like this
requestBody:
content:
multipart/form-data:
schema:
properties:
file:
description: file to upload
type: string
format: binary
additionalMetadata:
description: Additional data to pass to server
type: object <-- CHANGED FROM STRING
additionalProperties: true <-- ADDED
and run ./bin/generate-samples.sh ./bin/configs/kotlin-server-jaxrs-spec.yaml
the additionalMetadata field is dropped from PetApi
#POST
#Consumes("multipart/form-data")
#Produces("application/json")
suspend fun uploadFile(#PathParam("petId") petId: kotlin.Long, #FormParam(value = "file") fileInputStream: InputStream?): Response {
Is there a problem with how I'm specifying the request?

Open API schema conditional response field based on the presence of a query parameter

I am working on providing a GET REST API where I would like to conditionally include the total_documents field (its an integer count of the total number of records present in the DB table).
The API signature and response payload will be something like:
GET /endpoint/?total_documents&.....
Response Payload:
{
documents: [....],
total_documents: 100
}
Now I would like the total_documents field to be appeared in the response payload if and only if the total_documents query parameter exists in the URL.
This is what I tried, based on my schema:
fastify.addSchema({
$id: 'persistence-query-params',
title: "PersistenceQueryParams",
type: 'object',
description: 'Persistence Service GET API URL query specification. Applicable for GET API only.',
properties: {
'total_documents': {
description: 'Total number of documents present in the collection, after applying filters, if any. This query paramater does not take any value, just pass it as the name (e.g. &total_documents).',
nullable: true,
},
},
}
querystring: {
description: 'Persistence Service GET API URL query specification. Applicable for GET API only.',
$ref: 'persistence-query-params#',
},
response: {
200: {
properties: {
'documents': {
description: 'All the retrieved document(s) from the specified collection for the specified service database and account.',
type: 'array',
items: {
$ref: 'persistence-response-doc#',
}
},
'total_documents': {
description: "If total_documents query paremeter is specified, gives the total number of documents present in the collection, after applying query paramaters, if any. If total_documents is not specified, this field will not be available in the response payload.",
type: 'number',
default: -1,
},
},
dependencies: {
'total_documents': { required: ['querystring/properties/total_documents'] },
},
},
'4xx': {
$ref: 'error-response#',
description: 'Error response.'
}
}
What is the way out here?
Thanks,
Pradip
JSON Schema has no notion of a request or response or HTTP.
What you have here is an OpenAPI specification document.
The OpenAPI specification defines a way to access dynamic values, but only within Link Objects or Callback Objects, which includes the query params.
Runtime expressions allow defining values based on information that
will only be available within the HTTP message in an actual API call.
This mechanism is used by Link Objects and Callback Objects.
https://spec.openapis.org/oas/v3.1.0#runtime-expressions
JSON Schem has no way to reference instance data, let alone data relating to contexts it is unaware of.

backbone destroy not sending params

I've got a backbone model that I'm trying to destroy, but no params are being sent with the request, so the server is returning a 'Delete 404 not found' error.
I'll admit my structure is a bit strange as I'm creating/destroying the items based on if they are in a list already or not.
var list_item = new MyApp.Models.ListItem({item_id: this.model.id, group_id: this.model.group_id});
if($(e.currentTarget).hasClass('add')){
list_item.save(list_item, {
success: function(response){
this.model.attributes.addedtolist_id = response.id
console.log(this.model);
},
error: function(){
alert('could not save item');
}
});
} else if($(e.currentTarget).hasClass('remove')) {
list_item.id=this.model.addedtolist_id;
list_item.attributes.id = this.model.addedtolist_id;
console.log(list_item);
list_item.destroy({
success: function(){
alert('delete');
},
error: function(){
alert('could not uncheck');
}
});
}
the console output for list_item before destroy is
_escapedAttributes: Object
_previousAttributes: Object
_setting: false
attributes: Object
id: 2
item_id: 66
group_id: 64
__proto__: Object
cid: "c23"
id: 2
__proto__: q
but when I look at the headers sent with the delete request, I don't have any params being sent.
-----------------------update params being sent, 404 still being returned --------------
as per Yaroslav's recommendation, I've added a 'header' to the destroy method, but my rails controller is still returning a DELETE 404 not found error. I'm just trying to return the listitem to make sure i'm getting the right one before I destroy it.
My controller is
def destroy
listitem = Listitem.find(params[:id])
return render :json => listitem
end
I'd guess that you're setting the url in the model to a string:
Backbone.Model.extend({
url: '/list_items',
//...
});
That will tell Backbone to use exactly /list_items as the URL for all actions. You should use a function:
url: function() { return '/list_items/' + encodeURIComponent(this.id) }
or use a string with urlRoot and let the default url function add the id:
urlRoot: '/list_items'
What params are you expecting to be sent? Destroy makes just a http delete request by the url without body or any additional headers by default. The params argument is pased to the jquery ajax function, so you can specify headers there:
model.destroy({
...
headers : {
your_header : 123
}
})
list_item.destroy( **l** {
success: function(){
alert('delete');
}, ... );
Is that extra number one the problem?

Sencha touch 2 - show response (JSON string) on proxy loading

Is there a way to output the json-string read by my store in sencha touch 2?
My store is not reading the records so I'm trying to see where went wrong.
My store is defined as follows:
Ext.define("NotesApp.store.Online", {
extend: "Ext.data.Store",
config: {
model: 'NotesApp.model.Note',
storeId: 'Online',
proxy: {
type: 'jsonp',
url: 'http://xxxxxx.com/qa.php',
reader: {
type: 'json',
rootProperty: 'results'
}
},
autoLoad: false,
listeners: {
load: function() {
console.log("updating");
// Clear proxy from offline store
Ext.getStore('Notes').getProxy().clear();
console.log("updating1");
// Loop through records and fill the offline store
this.each(function(record) {
console.log("updating2");
Ext.getStore('Notes').add(record.data);
});
// Sync the offline store
Ext.getStore('Notes').sync();
console.log("updating3");
// Remove data from online store
this.removeAll();
console.log("updated");
}
},
fields: [
{
name: 'id'
},
{
name: 'dateCreated'
},
{
name: 'question'
},
{
name: 'answer'
},
{
name: 'type'
},
{
name: 'author'
}
]
}
});
you may get all the data returned by the server through the proxy, like this:
store.getProxy().getReader().rawData
You can get all the data (javascript objects) returned by the server through the proxy as lasaro suggests:
store.getProxy().getReader().rawData
To get the JSON string of the raw data (the reader should be a JSON reader) you can do:
Ext.encode(store.getProxy().getReader().rawData)
//or if you don't like 'shorthands':
Ext.JSON.encode(store.getProxy().getReader().rawData)
You can also get it by handling the store load event:
// add this in the store config
listeners: {
load: function(store, records, successful, operation, eOpts) {
operation.getResponse().responseText
}
}
As far as I know, there's no way to explicitly observe your response results if you are using a configured proxy (It's obviously easy if you manually send a Ext.Ajax.request or Ext.JsonP.request).
However, you can still watch your results from your browser's developer tools.
For Google Chrome:
When you start your application and assume that your request is completed. Switch to Network tab. The hightlighted link on the left-side panel is the API url from which I fetched data. And on the right panel, choose Response. The response result will appear there. If you have nothing, it's likely that you've triggered a bad request.
Hope this helps.
Your response json should be in following format in Ajax request
{results:[{"id":"1", "name":"note 1"},{"id":"2", "name":"note 2"},{"id":"3", "name":"note 3"}]}
id and name are properties of your model NOte.
For jsonp,
in your server side, get value from 'callback'. that value contains a name of callback method. Then concat that method name to your result string and write the response.
Then the json string should be in following format
callbackmethod({results:[{"id":"1", "name":"note 1"},{"id":"2", "name":"note 2"},{"id":"3", "name":"note 3"}]});

Sencha Touch - RESTful load() specific instance URL problem (Store/model)

It seems there is a problem with loading a specific instance (load() function) using the rest proxy in a model/store object. example:
Code:
Ext.regModel('User', {
fields: ['id', 'name', 'email'],
proxy: {
type: 'rest',
url : '/users'
}
});
//get a reference to the User model class
var User = Ext.ModelMgr.getModel('User');
//Uses the configured RestProxy to make a GET request to /users/123
User.load(123, {
success: function(user) {
console.log(user.getId()); //logs 123
}
});
This code is copied from Sencha touch's API. the generated URL is http://localhost/users?_dc=... instead of the desired (and documented) url http://localhost/users/123.
it also happens when using the store.load with a parameter.
Am I doing something wrong here?
Thanks
T
It seams the id parameter has been documented but not implemented. This has been discussed in the sencha forum [link]. A few non complete fixes are written in post #8 and post #13.