how to document swagger query parameter with json object input - json

How do I define a JSON object value for a request parameter that is in: query (not in: body)?
Example below:
paths:
/schedules:
get:
summary: Gets the list of schedules
description: |
The schedules endpoint returns information about the configured schedules.
parameters:
- name: filter
in: query
description: >
Returns whether alert runs on matching schedule.
Example request:
{
"type": "a",
"start" : "b",
"stop" : "c"
}
required: true
type: string
Because it's not in: body, I cannot use schema.

Related

AWS SSM - store multiple parameters using terraform and json file

We have a couple of legacy applications we're migrating to ec2 and these use a bunch of application configuration parameters. I need to be able to store each config as an individual parameter per application.
I'm trying the following but clearly not doing it right as it appends all values to a single parameter per application:
locals {
application = {
"application1" = { app_shortcode = "app1"},
"application2" = { app_shortcode = "app2"}
}
resource "aws_ssm_parameter" "application_parameters" {
for_each = local.application
name = each.key
value = jsonencode(file("${path.module}/${each.key}/ssm_param.json"))
}
my app1's ssm_param.json is something like
{
"app1_config1": "config_value_1",
"app1_config2": "config_value_2",
"app1_config3": "config_value_3"
}
and app2's ssm_param.jsonis
{
"app2_config_a": "config_value_a",
"app2_config_b": "config_value_b",
"app2_config_c": "config_value_c"
}
The current output is a single parameter like this for each application:
"{\r\n \"app2_config_a\": \"config_value_a\",\r\n \"app2_config_b\": \"config_value_b\"\r\n, \r\n \"app2_config_c\": \"config_value_c\"\r\n}"
Looking for suggestions please.
I solved this by using a slightly different approach (not quite the same as my initial one but this works for me for now):
used a ssm_params.yaml as below (the project team was kind enough to give me the config settings as yaml output)
parameter:
app1:
name: app1_config1
description: "application config test"
type: "String"
value: "some_randoM_value"
app1:
name: app_config2
description: "another test"
type: "SecureString"
value: "some_random123_value###"
app2:
name: app_config_2
description: "config test"
type: "String"
value: "some_randoM_value_2"
locals {
params = yamldecode(file("${path.module}/ssm_params.yaml"))
}
resource "aws_ssm_parameter" "app_params" {
for_each = local.params.parameter
name = each.value.name
type = each.value.type
value = each.value.value
}

JSON API property names and Swagger documentation for proper response modeling

I'm seeking the best way for documenting my rest API response according to JSON API and also how to describe it in Swagger YAML documentation.
My API receives a word and a list of URLs and returns how many times the given word appears in each website provided. My first take for the successful response was:
{
"data": {
"foo": {
"http://www.foobar.com": 12,
"http://www.bar.com": 0
}
}
}
Using the word and the URLs as property names.
I'd like to know if it is a good practice according to JSON API and if yes, how to document it in Swagger YAML:
swagger: '2.0'
info:
description: How many times a given word appears in each site provided? This API answer it
version: 1.0.0
title: WordApp
contact:
email: foo#bar.com
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
paths:
/:
get:
summary: returns how many times a given word appears in each provided URL
description: |
By passing in the appropriate options, you can search for
the number of occurrences of a given word in each URL provided
produces:
- application/json
parameters:
- in: query
name: word
description: pass a word for looking up its occurrences in each site
required: true
type: string
- in: query
name: url
description: a list of valid URLs
type: string
required: true
responses:
200:
description: search results matching criteria
schema:
type: array
items:
$ref: '#/definitions/Data'
500:
description: Internal Server Error
definitions:
Data:
type: object
required:
- word
- url
- value
properties:
word:
type: string
example: Brazil
url:
type: string
example: 'https://www.nytimes.com/'
value:
type: integer
example: 12
If it is not the best practice, what would be a better way of modeling my response?

Swagger / JSON — Optional fields being treated as required

I'm having some difficulty with Swagger 2.0 insiting that fields are required when they are not in fact defined to be required.
Here's a snipped version of my Swagger YML file.
definitions:
category:
type: object
required:
- name
- code
properties:
name:
type: string
description: Category name
code:
type: string
description: Category code
_links:
$ref: '#/definitions/categoryLinks'
children:
type: array
items:
$ref: '#/definitions/category'
categoryLinks:
required:
- self
- parent
- children
properties:
self:
description: Canonical link to this category
$ref: '#/definitions/genericLink'
parent:
description: Link to the parent category
$ref: '#/definitions/genericLink'
children:
description: Link to the child categories
$ref: '#/definitions/genericLink'
genericLink:
properties:
href:
type: string
description: The absolute URL being linked to.
paths:
'/categories/{category_code}':
get:
summary: Get a specific category
description: Returns information about a specific category.
parameters:
- name: category_code
description: Code of category to get
type: string
in: path
required: true
responses:
200:
description: Information about requested category.
schema:
$ref: '#/definitions/category'
The response from get '/categories/awesome-cat' looks like:
{
"name" => "My awesome Category",
"code" => "awesome-cat",
"_links" => {
"self" => {
"href" => "https://api.test.testing/categories/awesome-cat.json"
},
"parent"=> {},
"children" => {}
}
}
Which is as expected, and which conforms to my definiton of a category above.
I'm using the swagger rspec matcher called conform_to_the_documented_model_for provided by the Apivore project to validate the output from all my API endpoints:
matcher :conform_to_the_documented_model_for do |swagger, fragment|
match do |body|
body = JSON.parse(body)
#errors = JSON::Validator.fully_validate(swagger, body, fragment: fragment)
#errors.empty?
end
failure_message do |body|
#errors.map { |e| e.sub("'#", "'#{path}#").gsub(/^The property|in schema.*$/,'') }.join("\n")
end
end
Alas my test is failing.
3) the V1 API path /categories/{category_code} method get response 200 responds with the specified models
Failure/Error: expect(response.body).to conform_to_the_documented_model_for(swagger, fragment)
'#/_links/parent' did not contain a required property of 'href'
'#/_links/children' did not contain a required property of 'href'
'#' did not contain a required property of 'children'
For some reason the JSON validator is not regarding the href property of the link as optional, nor is it regarding rhe children property of category as optional.
It was my understanding that properties not listed under the required section are optional. Why is the JSON::Validator.fully_validate regarding them as non-optional?
Dave, just answering here too for completeness. The latest version of the Apivore gem, 0.0.2, no longer uses the :strict mode of the JSON Validator gem json-schema, which makes the assumption that all fields are required. This is a recent change I made after we sorted out the misunderstandings around additionalProperties. I am now of the opinion that the :strict mode of the validator isn't very helpful. The default JSON schema validation is correct, there is no reason to be any 'stricter'.
Making sure you have the latest version of the Apivore gem (0.0.2, or greater) should solve your problem.

Swagger UI 2.1 Stuck "fetching resource list"

I have a RESTful API that I have created recently and I won't remember how to use it in a few months. I decided to document my API using Swagger, however I'm going crazy.
I used http://editor.swagger.io/ to create the YAML file that I then convert into a JSON file Swagger can use. When I put file into Swagger UI it just gets stuck at fetching resource list: localhost/swagger.json and the console says Uncaught TypeError: Cannot read property '$ref' of undefined .
I'm using version 2.1.0-alpha.5 of Swagger UI.
Here is my spec file:
swagger: '2.0'
info:
title: TITLE
description: BLAH, BLAH, BLAH, ETC
version: "1.0b"
host: api.example.com
schemes:
- http
basePath: /v1
produces:
- application/json
paths:
/match.json:
get:
#summary: Match Data
description: Used for getting data about a match
parameters:
- name: id
in: query
description: The match ID of from a game
required: true
type: integer
format: int32
- name: key
in: query
description: API key used for authentication.
required: true
type: string
responses:
200:
description: Returns match data
schema:
type: array
items:
$ref: '#/definitions/MatchData'
default:
description: Unexpected error
schema:
$ref: '#/definitions/Error'
definitions:
MatchData:
properties:
info:
type: integer
format: int64
description: General information about the match
time:
type: integer
format: int64
description: Information about the start/end time
stats:
type: array
format: int64
description: Stats about the match
Error:
required:
- errorID
- message
properties:
errorID:
type: string
description: Error ID.
message:
type: string
description: Information about the error.
I've tested your spec, and while I'm not getting the same error you do, the spec is indeed invalid.
If you look at #/definitions/MatchData/properties/stats, you'll see that you define the type: array but you don't provide an 'items' property next to it to say which array it is (and that's mandatory). You may have intended to use type: integer like the properties above it, which goes along with the format: int64.
Since I don't know what you intended to provide, it's difficult to give an accurate solution, but if you add a comment with what you intended to do, I could provide a more detailed answer.
Upon some additional testing, I discovered that there's a bug in the UI. After you make that modification and the spec loads, the operation itself will not expand unless you click on the Expand Operations link. I've opened an issue about it, feel free to follow it there.
This problem can be due to some indentation errors in the yaml file which actually did not show up in the Swagger editor. Check all your definitions and whether they are getting displayed as expected in the preview that you can see in Swagger editor (especially check MatchData).
You can also try giving:
responses:
200:
description: Returns match data
schema:
type: array
items:
schema:
$ref: '#/definitions/MatchData'
For our case, we used Swagger-php and we have:
* #SWG\Response(
* response=200,
* description="app response"
* #SWG\Schema(
* type="array"
* )
* ),
but we missed " * #SWG\Items(ref="#/definitions/pet")". After removing "#SWG\Schema(", it works e.g.
* #SWG\Response(
* response=200,
* description="app response"
* ),

Rally JSON I/O error creating a test case result

I am trying to create a test case result using a REST client, but get this error:
"Errors": ["Cannot parse input stream due to I/O error as JSON document: Parse error: expected '{' but saw '\uFFFF' [ chars read = >>>\uFFFF<<< ]"]
I get the same error when the name of the object, testcaseresult is not specified in the request body. Here are the steps to create a test case result using a browser REST client:
a) Generate the authorize key using "GET" method and the following URL:
https://rally1.rallydev.com/slm/webservice/v2.0/security/authorize
This is the response that I get back, with the security token: "123abc..."
{"OperationResult": {"_rallyAPIMajor": "2", "_rallyAPIMinor": "0", "Errors": [], "Warnings": [], "SecurityToken": "abc123..."}}
b) Use "POST" method, and the following URL:
https://rally1.rallydev.com/slm/webservice/v2.0/testcaseresult/create?key=abc123...
notice the security token in the end.
c) here is an example of a request body:
{
"testcaseresult":
{
"Build":"1",
"Tester":"/user/777",
"Date":"2010-09-04T19:56:05.000Z",
"TestCase":"/testcase/1111",
"Verdict":"Pass"
}
}
Only the required fields and the fields you want to set need to be referenced. Notice the outer key/value pair
{
"testcaseresult":{}
}
The fields that point to a full object, like "Tester" (points to User object) and "TestCase" (points to a TestCase object that owns the result) have to be referenced by their ObjectIDs:
"Tester":"/user/777",
"TestCase":"/testcase/1111",