Swagger does not consider, that body is not required - http-post

I define endpoints with Swagger file, and for one of the endpoints the body (a JSON) is not required.
I describe it in this way in the yaml file:
- in: "body"
name: "body"
description: "some description here..."
required: false
schema:
type: string
After that I generate code from the Swagger file and for the body I get the following description in the method for this endpoint in the generated class:
#Valid #RequestBody String body
the generated file is an interface, and when I implement the method, and call the endpoint without setting a body, I get:
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing:
Can somebody tell me what I am doing wrong? When I put
#RequestBody(required=false) String body
, then everything works correct, but I would like this (required=false) to be generated automatically from the code generator...

Related

Swagger Codegen Java RequestBody Name

I am using the swagger-codegen Spring generator to generate the API classes using a YAML API definition. I am describing post request with multiple parameters in the body like this:
paths:
/api/test:
post:
...
requestBody:
content:
application/json:
schema:
properties:
firstName:
type: string
lastName:
type: string
This generates a method in the ApiDelegate with the signature ResponseEntity test(Body body). However, when describing multiple post methods like this, the subsequent generated signatures have parameters of type Body1, Body2, ... . With Swagger 2.0, it was possible to name the RequestBodies. Can I do something similar with V3?
Thanks for your help!
Kind regards

#AO\JsonContent in parameter of get request

In swagger's documentation using OpenApi specification, you can wrap schema in parameter to content with application/json:
parameters:
- in: query
name: filter
# Wrap 'schema' into 'content.<media-type>'
content:
application/json: # <---- media type indicates how to serialize / deserialize the parameter content
schema:
type: object
properties:
type:
type: string
color:
type: string
to send objects like this filter={"type":"t-shirt","color":"blue"}.
How to do it in swagger-php?
The swagger-editor and swagger-ui have added support for http://swagger.io/docs/specification/describing-parameters
The swagger-php library has added support too, it will be part of the 3.0.4 release.

Text formatting in Yaml "example:"

I'm trying to display some formatted text in a YAML file (in the example: key):
log_level_per_component:
type: object
example:
"{
\"Component1\": \"Info\",\n
\"Component2\": \"Debug\",\n
\"Component3\": \"Fatal\",\n
...\n
}"
for getting such a Swagger output:
"log_level_per_component": "{
"Component1": "Info",n
"Component2": "Debug",n
"Component3": "Fatal",n
...
}"
but I always get this:
"log_level_per_component": "{ \"Component1\": \"Info\",\n \"Component2\":
\"Debug\",\n \"Component3\": \"Fatal\",\n ...\n }"
Any idea what I can do to fix it?
Update 2018-05-17 for Helen's screenshot request about her solution:
This is what I get with both solutions by using "example:":
And this what I get by using "examples:":
Both are always empty.
YAML is a superset of JSON, so you can use the normal JSON object literal syntax in YAML:
log_level_per_component:
type: object
example:
{
"Component1": "Info",
"Component2": "Debug",
"Component3": "Fatal"
}
Alternatively, you can use YAML object syntax, and Swagger UI will render the example as JSON (provided that your request or response type is JSON):
log_level_per_component:
type: object
example:
Component1: Info
Component2: Debug
Component3: Fatal

Grails 3 - Spring Rest Docs using Rest assured giving SnippetException when using JSON views

I am trying to integrate Spring REST docs with rest assured with Grails 3.1.4 application. I am using JSON Views.
Complete code is at https://github.com/rohitpal99/rest-docs
In NoteController when I use
List<Note> noteList = Note.findAll()
Map response = [totalCount: noteList.size(), type: "note"]
render response as grails.converters.JSON
Document generation works well.
But I want to use JSON views like
respond Note.findAll()
where I have _notes.gson and index.gson files in /views directory. I get a SnippetException. A usual /notes GET request response is correct.
rest.docs.ApiDocumentationSpec > test and document get request for /index FAILED
org.springframework.restdocs.snippet.SnippetException at ApiDocumentationSpec.groovy:54
with no message. Unable to track why it occurs.
Please suggest.
Full stacktrace
org.springframework.restdocs.snippet.SnippetException: The following parts of the payload were not documented:
{
"instanceList" : [ {
"title" : "Hello, World!",
"body" : "Integration Test from Hello"
}, {
"title" : "Hello, Grails",
"body" : "Integration Test from Grails"
} ]
}
at org.springframework.restdocs.payload.AbstractFieldsSnippet.validateFieldDocumentation(AbstractFieldsSnippet.java:134)
at org.springframework.restdocs.payload.AbstractFieldsSnippet.createModel(AbstractFieldsSnippet.java:74)
at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:64)
at org.springframework.restdocs.generate.RestDocumentationGenerator.handle(RestDocumentationGenerator.java:192)
at org.springframework.restdocs.restassured.RestDocumentationFilter.filter(RestDocumentationFilter.java:63)
at com.jayway.restassured.internal.filter.FilterContextImpl.next(FilterContextImpl.groovy:73)
at org.springframework.restdocs.restassured.RestAssuredRestDocumentationConfigurer.filter(RestAssuredRestDocumentationConfigurer.java:65)
at com.jayway.restassured.internal.filter.FilterContextImpl.next(FilterContextImpl.groovy:73)
at com.jayway.restassured.internal.RequestSpecificationImpl.applyPathParamsAndSendRequest(RequestSpecificationImpl.groovy:1574)
at com.jayway.restassured.internal.RequestSpecificationImpl.get(RequestSpecificationImpl.groovy:159)
at rest.docs.ApiDocumentationSpec.$tt__$spock_feature_0_0(ApiDocumentationSpec.groovy:54)
at rest.docs.ApiDocumentationSpec.test and document get request for /index_closure2(ApiDocumentationSpec.groovy)
at groovy.lang.Closure.call(Closure.java:426)
at groovy.lang.Closure.call(Closure.java:442)
at grails.transaction.GrailsTransactionTemplate$1.doInTransaction(GrailsTransactionTemplate.groovy:70)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at grails.transaction.GrailsTransactionTemplate.executeAndRollback(GrailsTransactionTemplate.groovy:67)
at rest.docs.ApiDocumentationSpec.test and document get request for /index(ApiDocumentationSpec.groovy)
REST Docs will fail a test if you try to document something that isn't there or fail to document something that is there. You've documented two fields in your test:
responseFields(
fieldWithPath('totalCount').description('Total count'),
fieldWithPath('type').description("Type of result")
)))
REST Docs has failed the test as some parts of the response haven't been documented. Specifically an instanceList array that contains maps with two keys: title and body. You can document those and the other two fields with something like this:
responseFields(
fieldWithPath('totalCount').description('Total count'),
fieldWithPath('type').description("Type of result"),
fieldWithPath('instanceList[].title').description('Foo'),
fieldWithPath('instanceList[].body').description('Bar')
)))
If you don't care about potentially missing fields, you can use relaxedResponseFields instead of responseFields:
relaxedResponseFields(
fieldWithPath('totalCount').description('Total count'),
fieldWithPath('type').description("Type of result")
))
This won't fail the test if some fields are not mentioned.

savon-multipart - How to obtain attachments

I am using savon-multipart https://github.com/savonrb/savon-multipart to request a SOAP multipart response with an attachment (PDF). So far, this is my code:
require "savon-multipart"
client = Savon.client(
wsdl: "http://something.de?wsdl",
wsse_auth: [username: "uu", password: "??"]
)
reponse = client.call(:get_report, message: {
pdfId: 1
})
response.attachments
Authentication works fine. I can also fetch the XML-reponse. What I can't do is extract the attachment. There does not seem to exist a method for it.
According to savon-multipart's documentation
response.attachments
should contain the attachment(s). Unfortunately ruby tells me that this method is not defined.
I could't find an example implementation of savon-multipart so I'm coming to you guys :) Hope you can help me.
We had this same problem in some code. I hope this saves someone else some time in finding the solution.
When using savon-multipart, we had to add multipart: true to the parameters in call. When that parameter was added the response returned was of type Savon::Multipart::Response which has the attachments and parts methods.
reponse = client.call(:get_report, message: {
pdfId: 1
}, multipart: true)
Without that parameter, or with it set to false, the returned response is a Savon::Response object which does not have those methods.

Resources