I am trying to run my swagger file and i keep getting this error.
Error: ptr must be a JSON Pointer
at pathFromPtr (/Users/salma/Desktop/swaggerIntegration/node_modules/json-refs/index.js:1128:11)
at /Users/salma/Desktop/swaggerIntegration/node_modules/json-refs/index.js:293:45
at process.runNextTicks [as _tickCallback] (internal/process/next_tick.js:47:5)
at Function.Module.runMain (internal/modules/cjs/loader.js:800:11)
at executeUserCode (internal/bootstrap/node.js:526:15)
at startMainThreadExecution (internal/bootstrap/node.js:439:3)
and here is my swagger file
swagger: "2.0"
info:
version: "0.0.1"
title: employees DB
# during dev, should point to your local machine
host: localhost:10010
# basePath prefixes all resource paths
basePath: /
#
schemes:
# tip: remove http to make production-grade
- http
- https
# format of bodies a client can send (Content-Type)
consumes:
- application/json
# format of the responses to the client (Accepts)
produces:
- application/json
paths:
/employees:
# binds a127 app logic to a route
x-swagger-router-controller: employees
get:
description: Returns 'Hello' to the caller
# used as the method name of the controller
operationId: index
parameters:
- name: name
in: query
description: The name of the person to whom to say hello
required: false
type: string
responses:
"200":
description: Success
schema:
# a pointer to a definition
$ref: "#/definitions/employeesListBody"
# responses may fall through to errors
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
/swagger:
x-swagger-pipe: swagger_raw
# complex objects have schema definitions
definitions:
employeesListBody:
required:
- employees
properties:
employees:
type: array
items:
$ref: "#definitions/Employee"
Employee:
required:
- name
- email
- position
properties:
name:
type: string
email:
type: string
position:
type: string
age:
type: integer
minimum: 20
ErrorResponse:
required:
- message
properties:
message:
type: string
any idea how to solve that ?
is there an easier way to prettify the swagger file? because i get many parsing erros.
also does any one have a good example of using swagger with express and mongodb ?
many thanks.
One of the references is missing a / after #:
$ref: "#definitions/Employee"
Change it to:
$ref: "#/definitions/Employee"
# ^
If you paste your definition into http://editor.swagger.io, it shows where exactly the error is.
Related
I'm trying to create an S3 Bucket and a corresponding Resource Policy in the same serverless.yml so that both are established on the new stack formation.
However, I am running into an error on build:
Unresolved resource dependencies [CUSTOM-BUCKETNAME] in the Resources block of the template
Is there to a way to synchronously create the policy so that it waits for the bucket to be created first? I'm setting this up in the resources section of my yml
resources:
Resources:
Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: CUSTOM-BUCKETNAME
BucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket:
Ref: CUSTOM-BUCKETNAME
PolicyDocument:
Statement:
- Principal:
Service: "ses.amazonaws.com"
Action:
- s3:PutObject
Effect: Allow
Sid: "AllowSESPuts"
Resource:
Fn::Join: ['', ['arn:aws:s3:::', Ref: "CUSTOM-BUCKETNAME", '/*'] ]
Above is a small snippet of my yml configuration.
After using DependsOn, I'm still getting the same error. Worth note, the resource dependency refers to the dynamic name (CUSTOM-BUCKETNAME) of the bucket.
resources:
Resources:
Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: CUSTOM-BUCKETNAME
BucketPolicy:
Type: AWS::S3::BucketPolicy
DependsOn: Bucket
Properties:
Bucket:
Ref: CUSTOM-BUCKETNAME
PolicyDocument:
Statement:
- Principal:
Service: "ses.amazonaws.com"
Action:
- s3:PutObject
Effect: Allow
Sid: "AllowSESPuts"
Resource:
Fn::Join: ['', ['arn:aws:s3:::', Ref: "CUSTOM-BUCKETNAME", '/*'] ]
CUSTOM-BUCKETNAME is never explicity hardcoded in the yml itself, its a dynamically generated name using template literals.
Issue is occurring on your policy as your bucket is: BucketName: CUSTOM-BUCKETNAME
Not a referenced parameter. Which means your not referencing the actual resource in the policy statement when your using Bucket: Ref: CUSTOM-BUCKETNAME.
Instead, either change the bucket name to reference the same parameter BucketName: Ref: CUSTOM-BUCKETNAME or change the policy to reference the resource: Bucket: Ref: Bucket
CloudFormation DependsOn attribute should solve your problem.
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-dependson.html
I am having a problem with setting up the swagger into my node.js application. I am using swagger-jsdoc and swagger-ui-express for creating documentation. Here are the versions
"swagger-jsdoc": "3.5.0", "swagger-ui-express": "4.1.3"
Below is the configs, which I pass to swagger-jsdoc.
openapi: 3.0.0
info:
description: test
version: 3.0.0
title: U-CRM api documentation
termsOfService: http://swagger.io/terms/
servers:
- url: 'https://localhost:5000'
description: Local server
tags:
- name: U-CRM
description: CRM for university
components:
parameters:
$ref: 'components/parameters/index.yml'
schemas:
$ref: 'components/schemas/index.yml'
paths:
$ref: '#/paths/index.yml'
After all, I get an error
Cannot read property 'parameters' of undefined
Actually, it is surprising to me, as I read swagger docs carefully.
What could be the problem?
OpenAPI does not support $ref everywhere. $ref can only be used in specific places where the OpenAPI Specification explicitly states that the value of a field can be a "Reference Object".
For example, $ref is not allowed directly under paths, under components/parameters and components/schemas - you can only reference individual paths, parameters and schemas.
The correct version of your example is:
paths:
/foo:
$ref: '#/paths/index.yml#/~1foo' # $ref to root node `/foo` in `paths/index.yml`
/bar:
$ref: '#/paths/index.yml#/~1bar' # $ref to root node `/bar` in `paths/index.yml`
components:
parameters:
param1:
$ref: 'components/parameters/index.yml#/param1'
param2:
$ref: 'components/parameters/index.yml#/param2'
schemas:
schema1:
$ref: 'components/schemas/index.yml#/schema1'
schema2:
$ref: 'components/schemas/index.yml#/schema2'
If you want to use $ref in random places, you'll have to pre-process your definition using a parser/tool that can resolve arbitrary $refs; this will give you a valid OpenAPI file that can be used with OpenAPI-compliant tools. One such pre-processing tool is json-refs, you can find an example of pre-processing here.
REST Api services is available in Node.js express app along with routes are specified properly. I am planning to use node.js to auto generate the swagger YAML Open API specification. Writing a swagger YAML manually is tough if you have more endpoints and the intention to add more endpoints will need to update everywhere.
Example: Consider a REST API services contains two basic endpoints and specified in the following YAML
swagger: "2.0"
info:
version: "0.0.1"
title: Hello World App
# during dev, should point to your local machine
host: localhost:10010
# basePath prefixes all resource paths
basePath: /
#
schemes:
# tip: remove http to make production-grade
- http
- https
# format of bodies a client can send (Content-Type)
consumes:
- application/json
# format of the responses to the client (Accepts)
produces:
- application/json
paths:
/hello:
# binds a127 app logic to a route
x-swagger-router-controller: hello_world
get:
description: Returns 'Hello' to the caller
# used as the method name of the controller
operationId: hello
parameters:
- name: name
in: query
description: The name of the person to whom to say hello
required: false
type: string
responses:
"200":
description: Success
schema:
# a pointer to a definition
$ref: "#/definitions/HelloWorldResponse"
# responses may fall through to errors
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
/try:
x-swagger-router-controller: hello_world
get:
description: Returns 'Try' to the caller
operationId: try
parameters:
- name: name
in: query
description: The content to try
required: false
type: string
responses:
"200":
description: Success
schema:
$ref: "#/definitions/HelloWorldResponse"
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
/swagger:
x-swagger-pipe: swagger_raw
# complex objects have schema definitions
definitions:
HelloWorldResponse:
required:
- message
properties:
message:
type: string
ErrorResponse:
required:
- message
properties:
message:
type: string
My doubt is how can i generate this YAML in Node.js? Is there any specific NPM package or any swagger module available to generate this kind of YAML or writing code on own is preferable?
Trying to analyze the better way to do this. I appreciate all suggestions and thanks in advance!
I am looking for help debugging the message "Field Errors" I receive as a browser popup when trying to upload an image through the Keystone CMS.
I am using the npm package keystone-storage-adapter-s3. For some context, I am trying to upload images to an AWS S3 bucket and later retrieve them as part of a website's content using the Keystone CMS. I am pretty new to AWS S3, but trying.
Here is the image model in question.
const keystone = require('keystone');
const Types = keystone.Field.Types;
const Image = new keystone.List('Image');
const storage = new keystone.Storage({
adapter: require('keystone-storage-adapter-s3'),
s3: {
key: process.env.S3_KEY, // required; defaults to process.env.S3_KEY
secret: process.env.S3_SECRET, // required; defaults to process.env.S3_SECRET
bucket: process.env.S3_BUCKET, // required; defaults to process.env.S3_BUCKET
region: process.env.S3_REGION, // optional; defaults to process.env.S3_REGION, or if that's not specified, us-east-1
uploadParams: { // optional; add S3 upload params; see below for details
ACL: 'public-read',
},
},
schema: {
bucket: true, // optional; store the bucket the file was uploaded to in your db
etag: true, // optional; store the etag for the resource
path: true, // optional; store the path of the file in your db
url: true, // optional; generate & store a public URL
},
});
Image.add({
name: { type: String },
file: { type: Types.File, storage: storage },
});
Image.register();
I believe I've filled out the region, bucket name, secret (random secure string), and even created a new key that's stored securely as well in a .env file.
Here is the error I receive in the browser console.
packages.js:33 POST http://localhost:3000/keystone/api/images/5bf2c27e05ba79178cd7d2be 500 (Internal Server Error)
a # packages.js:33
i # packages.js:33
List.updateItem # admin.js:22863
updateItem # admin.js:15021
r # packages.js:16
a # packages.js:14
s # packages.js:14
d # packages.js:14
v # packages.js:14
r # packages.js:17
processEventQueue # packages.js:14
r # packages.js:16
handleTopLevel # packages.js:16
i # packages.js:16
perform # packages.js:17
batchedUpdates # packages.js:16
i # packages.js:16
dispatchEvent # packages.js:16
These are the permission settings of my S3 bucket.
Block new public ACLs and uploading public objects: False
Remove public access granted through public ACLs: False
Block new public bucket policies: True
Block public and cross-account access if bucket has public policies: True
These are similar questions, but I believe have to do with Keystone's previous implementation of Knox.
"Field errors"
Field errors in s3 file upload
I found the debug package in use within node_modules/keystone/fields/types/file/FileType.js and enabled it. I received the following debug messages when attempting to upload an image.
$ DEBUG=keystone:fields:file node keystone.js
------------------------------------------------
KeystoneJS v4.0.0 started:
keystone-s3 is ready on http://0.0.0.0:3000
------------------------------------------------
GET /keystone/images/5bf2c27e05ba79178cd7d2be 200 17.446 ms
GET /keystone/api/images/5bf2c27e05ba79178cd7d2be?drilldown=true 304 3.528 ms
keystone:fields:file [Image.file] Validating input: upload:File-file-1001 +0ms
keystone:fields:file [Image.file] Validation result: true +1ms
keystone:fields:file [Image.file] Uploading file for item 5bf2c27e05ba79178cd7d2be: { fieldname: 'File-file-1001',
originalname: 'oof.PNG',
encoding: '7bit',
mimetype: 'image/png',
destination: 'C:\\Users\\Dylan\\AppData\\Local\\Temp',
filename: '42c161c1c36a84a244a2cf09d327afd4',
path:
'C:\\Users\\Dylan\\AppData\\Local\\Temp\\42c161c1c36a84a244a2cf09d327afd4',
size: 6684 } +0ms
POST /keystone/api/images/5bf2c27e05ba79178cd7d2be 500 225.027 ms
This message looks promising, so I will keep looking through this to see if I can debug any more information.
Edit: Progress! I searched the Keystone package for "Field errors" and found where the error message is set. Debugging that location revealed another error.
"InvalidAccessKeyId: The AWS Access Key Id you provided does not exist in our records."
The search continues.
I was mixing up my "key" and "secret".
As per the keystone-storage-adapter-s3 package, required are your "key" and "secret". Having inexperience with AWS, and some with web development, I thought the secret was a random secure string (like you would sign a cookie with) and the key was my secret key.
wrong
"key" : Secret Key
"secret" : Random secure key.
correct
"key": Key ID
"secret": Secret key.
Turns out I was wrong. The "key" is my key id, and the "secret" is my secret key. Settings those correctly in my .env file allowed me to upload a file to the S3 bucket.
I found this code from yogiben:autoform-map package,
what does # mean when you use it in front of a meteor collection?
Schemas = {}
#Cities = new Meteor.Collection('cities');
Schemas.Cities = new SimpleSchema
name:
type:String
max: 60
location:
type: String
autoform:
type: 'map'
afFieldInput:
geolocation: true
searchBox: true
autolocate: true
Cities.attachSchema(Schemas.Cities)
It has nothing to do with Meteor collection. The code is written in CoffeeScript. The # in front denotes that Cities is a global object. From the coffee script website,
As a shortcut for this.property, you can use #property.