How to send a JSON array in multipart/form-data in Swagger UI? - multipartform-data

I’m trying to send an array of objects through multipart/form-data:
post:
summary: Creates a user
requestBody:
content:
multipart/form-data:
schema:
type: object
properties: # Request parts
id:
type: string
format: uuid
address: # <---------
type: array
items:
type: object
properties:
street:
type: string
city:
type: string
profileImage:
type: string
format: base64
But Swagger UI sends the address array incorrectly - as {},{} instead of [{},{}], that is, without the enclosing square brackets:
I even tried encoding it separately as JSON.
What am I missing, please?

i later got it to work by adding example
post:
summary: Creates a user
requestBody:
content:
multipart/form-data:
schema:
type: object
properties: # Request parts
id:
type: string
format: uuid
address:
type: array
items:
type: object
properties:
street:
type: string
city:
type: string
example:
- street: Jones Street, Manhattan
city: New York
- street: Hollywood Boulevard
city: Los Angeles
profileImage:
type: string
format: base64
my observation is just send or modify what you already have in the example, adding new ones won't be correctly formatted. that is, if you have two items in an array work with those two do not add extra items

Related

How we can add custom fields to mongodb database?

Hello guys I need your help,
I am making a simple app where users can create a post about their favorite person so there are different fields to write text, now I've made the functionality for the user to create any new field with a title of their choice like if a person "a" want to add a new field with name age and custom "b" wants to add a new field with name country, so I've made this but how can I get that custom data automatically in my database so if the user will create a new field with any name I want that field and text written in it in my database. Can you help me with that?
and please try to explain to me in a simple manner am just a beginner with mongoDB.
there was a guy who told me to do this with $addFields aggregation but he didn't tell me anything else I saw many documents but am not getting How can I make this done :(
thank you sooooo much in advance. :)
here's my schema for default fields:
const personSchema = new Schema({
firstName: String,
lastName: String,
birthDay: String,
gender: String,
pronouns: String,
relationship: String,
user: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
username: String,
}
});
You simply have to pass the strict option to false while you create your schema!
By default, mongoose will set this option to true so you can't send any unnecessary information to DB!
const personSchema = new Schema({
firstName: String,
lastName: String,
birthDay: String,
gender: String,
pronouns: String,
relationship: String,
user: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
username: String,
}
}, { strict: false });
https://mongoosejs.com/docs/guide.html#strict

Object Fields Missing in Autogenerated Mutation Input for GraphQL AppSync

I have a custom GraphQL schema for a blogging application for my ReactJS app. The main model type is Post.
Post contains a number of properties that have a custom type eg Gallery: [MediaObject]. The majority of these properties with custom types are missing from the automatically generated CreatePostInput by AWS Amplify CLI. This means that I cannot create a Post object that has these properties with custom type.
The lack of these custom type properties is evident in the AppSync console, under Queries. In the Explorer, go to Mutations, click on CreatePost mutation, and you will see that 6 of the 21 properties for Post are missing. The missing properties are tags, attributes, seo, featuredImage, gallery and createdBy.
They are also missing in the autogenerated graphql schema in api > build > schema.graphql in the react app.
I have made multiple clean projects from scratch, and the problem is reproducible. I have updated the amplify cli to 4.51.2 and still no luck.
I would expect the CreatePostInput to have the associated inputs for all of the fields, not just comments (see the autogenerated CreatePostInput below) eg: attribute: AttributeInput, but these are not generated either. This would allow me to create a Post object that has these custom fields populated. However, only the CommentInput has been generated. What am I doing wrong here, or have I misunderstood something?
Thanks!
Below is my gql schema and both the autogenerated CreatePostInput and CommentInput:
type Post #model {
id: ID
createdBy: User
createdAt: AWSDateTime
updatedAt:AWSDateTime
type: PostType!
title: String!
status: Status
content: String!
excerpt: String
slug: AWSURL
wordpressId: Int
wordpressImageURLs: [AWSURL]
author: String
likes: Int
tags: [Tag]
attributes: Attribute
seo: Seo
featuredImage: MediaObject
gallery: [MediaObject]
comments: [Comment]
numberOfComments: Int
}
type User #model {
id: ID
username: String!
firstName: String
lastName: String
email: AWSEmail!
avatar: MediaObject
location: String
createdAt: AWSDateTime
updatedAt: AWSDateTime
Posts: [Post]
}
type Tag #model {
id: ID
postIds: [ID]
name: String!
}
type Attribute #model {
id: ID
postId: ID
link: String
imgUrl: AWSURL
ogImage: AWSURL
ogDescription: String
canonicalLink: AWSURL
}
type Seo #model {
id: ID
postId: ID
metaRobotsNoIndex: String
metaRobotsNoFollow: String
canonical: AWSURL
metaDesc: String
opengraphDescription: String
opengraphModifiedTime: AWSDateTime
opengraphPublishedTime: AWSDateTime
opengraphTitle: String
opengraphUrl: AWSURL
opengraphImage: AWSURL
twitterDescription: String
twitterImage: String
twitterTitle: String
schemaSeo: String
}
type Comment
{
id: ID
postId: ID!
createdBy: ID
author: String
createdAt: AWSDateTime
text: String!
likes: Int
}
type MediaObject #model {
id: ID
linkedId: ID
createdBy: ID
mediaItemUrl: AWSURL
srcSet: String
medium: AWSURL
thumb: AWSURL
sourceURL: AWSURL
description: String
bucket: String
region: String
key: String
type: MediaObjectType
}
type Like #model {
id: ID!
objectVotedOnID: ID!
createdBy: ID
createdAt: AWSDateTime
likeOn: LikeOnType
}
enum PostType {
TOOLS
BLOGS
INSPIRATIONS
NEWS
POSTS
NEWSLETTERS
}
enum LikeOnType {
POST
COMMENT
}
enum Status {
PUBLISH
DRAFT
}
enum MediaObjectType {
IMAGE
VIDEO
AUDIO
}
Autogenerated (only CreatePostInput and CommentInput have been included for brevity), found in amplify > backend > api > build > schema.graphql:
input CreatePostInput {
id: ID
createdAt: AWSDateTime
updatedAt: AWSDateTime
type: PostType!
title: String!
status: Status
content: String!
excerpt: String
slug: AWSURL
wordpressId: Int
wordpressImageURLs: [AWSURL]
author: String
likes: Int
comments: [CommentInput]
numberOfComments: Int
}
input CommentInput {
id: ID
postId: ID!
createdBy: ID
author: String
createdAt: AWSDateTime
text: String!
likes: Int
}
At least for the example, you cited, Post and MediaObject have a one-to-many relationship. That is, a single Post has many MediaObjects. You won't see the many relations of a one-to-many generated on the parent object for mutations.
When creating a parent object that has children, you'll need to do this in 2 steps.
Create the parent object, and return the id.
Using the parentId, create the N child objects.
Here is some documentation from Amplify on how to do this: https://docs.amplify.aws/cli/graphql-transformer/connection#has-many
To anyone else that notices this strange behaviour: The reason why CommentInput is included in the CreatePostInput, whilst the other custom types are not, is because Comment was the only type that I had forgot to label with an #model directive. I assume that this means that AppSync then views this type on the comments field as a set of nested fields, rather than an object with its own DynamoDB table. I have asked the AWS Amplify team and will update this answer when I hear more from them.

Formik & Yup - How to make a schema for an array and string

I am using Formik to create a form of a book library where each item in the list would look like:
author: {
name: 'string',
titles: ['string']
}
I am having trouble trying to make a schema with Yup to validate those fields. I have:
schema = Yup.object().shape({
author: Yup.object().shape({
name: Yup.string().min().max().required('...')
}),
author: Yup.array().of(
Yup.object().shape({
titles: Yup.string().min().max().required('...')
})
)
});
The initial values are also:
{
author: {name: ''},
author: [{ titles: '' }]
}
My validation works for the array but not the name. I am assuming the issue is that I cannot have the same name for both of the elements in the object, however I do not know how to combine the two fields in both the initial values as well as the schema. I looked through Yup API and I noticed they had mixed() but I didn't understand how to implement with what I need. Is this way possible or would having validation for both of these possible?
Author is not an array, it's an object which contains an array and a string. Also, you're defining it twice:
schema = Yup.object().shape({
author: Yup.object().shape({
name: Yup.string().min().max().required('...'),
titles: Yup.arrayOf(Yup.string())
}),
});
Confused about why you have 2 author keys. The schema above corresponds to:
{
author: {
name: "hehyryg",
titles: ["title", "title2"]
}
}

flow nested object values not accesible

I'm trying to access deeply nested values from an object but i'm getting the following error in flow:
Property cannot be accessed on property 'author' of unknown type
type ARTICLE_TYPE = {
id: number,
authorId: number,
type: 'article' | 'video' | 'audio' | 'perspective',
title: string,
preview: string,
imageUrl: ?string,
date: string,
}
type AUTHOR_TYPE = {
company: string,
id: number,
name: string,
profileImage: string
}
type TileProps = {
...ARTICLE_TYPE,
...{
author: AUTHOR_TYPE,
},
imageAspectRatio: string
}
I suspect it might be something to do with the way i'm defining the type but it seems ok to me:
Relevant testable code is Here
Any help is appreciated!
I suggest you don't use spread operators with flow types, rather use type concatenation.
type TileProps = ARTICLE_TYPE & {
author: AUTHOR_TYPE,
imageAspectRatio: string
}
Make life easy on yourself and those who read your code after you.

RAML 1.0 Array of inner types

I have an api with two main resources : Project and Product, they are two differents resources with only a link between them.
Product:
type: object
properties:
name: string
projects: ProjectReference[]
ProjectReference:
type: object
properties:
identifier: string
links: Links[]
Project:
type: object
properties:
identifier: string
members: string[]
product:
type: object
properties:
name: string
links: Link[]
Link:
type: object
properties:
rel: string
href: string
I would like to inline the ProjectReference type into Product. However, I have not found how to create array of inline types.
Can we create array of inline types with RAML 1.0 ?
Not sure what is the advantage of inlining....but you could do this:
Product:
type: object
properties:
name: string
projects:
type: array
items:
type: object
properties:
identifier: string
links: Links[]

Resources