Let's say I have a user model defined with: id, first_name, last_name, created_at, and I have an object definition for it in swagger.
A POST request to create a user would expect first_name and last_name. How would I write that in swagger? Swagger expects a single object for the Should I create a new object just for the that?
How about
myuser:
allOf:
- $ref: '#/definitions/myuserbase'
- type: object
properties:
id:
description: |
Unique identifier for the classifier.
type: string
example: user1
created_at:
description: |
Date and time the user was created.
type: string
format: date-time
required:
- id
and
myuserbase:
type: object
properties:
first_name:
description: |
First name of the user
type: string
last_name:
description: |
Last name of the user
type: string
required:
- first_name
- last_name
You can mark first_name, last_name as the required fields. Here is an example:
required:
- first_name
- last_name
Ref: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/test/resources/2_0/petstore.yaml#L658-L660
(in other words, no need to create a separate object)
Related
I'm coding a search function, with users having a username that you can find by searching. I can get users that completely match the string query, but I want to get all users whose username contains the query string I entered.
Here's what I have so far:
paths:
/user:
description: get and create users
get:
description: Get user
parameters:
# something here to get all users whose name includes the query
- in: query
name: studentname
required: true
schema:
type: string
example: Jeffery
responses:
200:
description: Successful!
content:
application/json:
schema:
$ref: '#/components/schemas/students'
I am trying to create an api rails application and i am encountering some problemes.
i got two models
A Shop with the following attributes name:string, localisation:text, size:integer
Employee with the following attributes name:string social_number:integer age:integer shop:references
A Shop can have multiple references so i thought about using belongs_to in the Employe modeland has_many in the Shop model
I am trying to create the following request POST /shops
the response should show the followings about the data of the shops
{
name: String,
localisation: text,
age: integer,
employees: List[employee]
}
And concerning the data of an employee
{
name: String,
social_number: integer,
age:integer
}
I know that I can write Shop.employees.create(name: "Yves", social_number: "12342323", age: 29) and I can see that it works in the DB.
But when I am writing the post request in the create function, how can I show the employee attribute in the body response? I can only show the attributes of the shops i.e name, localisation, age.
Thanks to the belongs_to association, I have the shop_id in the table of Employees but I don't have anything in the Shop table. How can I make a link in order to show the employees characteristics that belongs to the shop?
I'm using Rails.
Thanks all, and sorry for the long post.
Tell me if I was not clear.
Personally I would have different API calls to create a shop and an employee. So call it to create a shop, and get the shop id back. Then use this shop id in following calls to create the employees.
Alternatively, creating a shop with an number of employees, send the employee attributes as an array in the create shop request.
{
name: String,
localisation: text,
age: integer,
employees: [
{
name: String,
social_number: integer,
age:integer
},
{
name: String,
social_number: integer,
age:integer
}
]
}
The response should be similar but with ids.
{
id: integer,
name: String,
localisation: text,
age: integer,
employees: [
{
id: integer,
name: String,
social_number: integer,
age:integer
},
{
id: integer,
name: String,
social_number: integer,
age:integer
}
]
}
The employee hashes might optionally include the shop_id attribute. It doesn't hurt anything.
Here is an API call I am writing. It works with INTEGER. However in the PostgreSQL DB the Hospital_ID is BIGINT.
desc "All Hospital Locations"
params do
requires :authtoken, type: String, desc: "auth token"
requires :hospitalid, type: Integer, desc: "hospital ID"
#requires :hospitalid, type: Virtus::Attribute::Bigint, desc: "hospital ID"
#Virtus::Attribute::Boolean,
end
I want to be able to pass BIGINT or similar variable.
I have 3 classes with STI and I want to load fixtures for each:
Employee
Admin < Employee
OtherEmployee < Employee
My test_helper files has fixtures :all
and the fixtures are in:
employees.yml
admins.yml
other_employees.yml
and yet only the data from other_employees.yml is loaded into the database. Any idea why or how to fix this?
Fixtures:
#employees.yml
one:
id: 1
name: Name1
full_name: FName1
type: Admin
two:
name: Name2
full_name: FName2
type: Admin
three:
name: Name3
full_name: FName3
type: OtherEmployee
#admins.yml
adminone:
name: Admin1
full_name: FAdmin1
type: Admin
admintwo:
name: Admin2
full_name: FAdmin2
type: Admin
#other_employees.yml
oeone:
name: Oemp1
full_name: FOemp1
type: OtherEmployee
oetwo:
name: Oemp2
full_name: FOemp2
type: OtherEmployee
Which version of Rails are you using? It appears that this was a bug and was fixed in Rails 5 and 4.2
https://github.com/rails/rails/issues/18492
There is also some info in the last comment that may be helpful.
https://github.com/rails/rails/issues/18492#issuecomment-218479248
For anyone still running into this problem you need to add a type
attribute to your fixtures, which represents the STI attribute on your
model. For the provided example this would be:
#teachers.yml
presto:
name: Test Teacher
type: Teacher
#students.yml
testo:
name: Test Person
teacher: presto
type: Student
Originally, I specified a relationship where contact has_many services. Therefore, services has a foreign key of contact_id:
class Contact
include Mongoid::Document
field :name, type: String
end
class Service
field :name, type: String
field :contact_id, type: Integer
end
Now there is a possibility to add an additional contact to a service, so service has many contacts. However, the contacts that are added are ones that already exist independently. So I do not want to embed one entity inside another. A contact and service will always live independently. No embedding.
So should I just store the ids of the contacts inside an array of Service? In other words, my new models will look like this:
class Contact
include Mongoid::Document
field :name, type: String
end
class Service
field :name, type: String
field :contact_id, type: Integer
field :contact_ids, type: Array, default: []
end
Or is there a better solution to address the many to many problem here (without embedding one document in another)?
For the Many-To-Many, you don't have 36 options : you actually have 2 :
Array of IDs on One side like you did
Array of IDs on Both sides.
The cool thing with the "both sides" solution is that you can find query documents from both collections to get the links.
Example with books and authors :
db.books.findOne()
{
_id: 1,
title: "The Great Gatsby",
authors: [1, 5]
}
db.authors.findOne()
{
_id: 1,
firstName: "F. Scott",
lastName: "Fitzgerald",
books: [1, 3, 20]
}