Structural error property type should be equal to one of the allowed values allowedValues: string, number, boolean, integer, array in Swagger editor - swagger-2.0

I'm using springfox 2.9.2 and want test my swagger JSON as YAML in https://editor.swagger.io/
I have property with #ApiParam annotation type: object
#ApiParam(value = "metadata file")
protected Object metadataFile;
but when I test my generated json on swagger editor I got this error:
Structural error at ---.parameters.5.type should be equal to one of
the allowed values allowedValues: string, number, boolean, integer,
array Jump to line ---
there is way to allowed property type object in this section?
the section that trigger the problem
paths:
:
post:
parameters:
name: metadataFile
in: query
description: ...
required: false
type: object

Swagger documentation (go to the bottom of the page): https://swagger.io/docs/specification/2-0/describing-parameters/
"Can I have an object as a query parameter?
This is possible in OpenAPI 3.0, but not in 2.0."

Related

serverless step functions: Getting error when passing more than one fields in the payload for lambda

Error: Invalid State Machine Definition: 'SCHEMA_VALIDATION_FAILED: The value for the field 'Date.$' must be a valid JSONPath at /States/Insert Data Dynamodb/Parameters' (Service: AWSStepFunctions; Status Code: 400; Error Code: InvalidDefinition;
below is the corresponding serverless.yaml code.
I tried wrapping the two parameters into encoded json string and passed it as single payload field and it resulted in the same error but when there is only one plain field in the payload this code deployed successfully
Any suggestions on how to pass two parameters?
service: service-name
frameworkVersion: '2'
provider:
name: aws
runtime: go1.x
lambdaHashingVersion: 20201221
stage: ${opt:stage, self:custom.defaultStage}
region: us-east-1
tags: ${self:custom.tagsObject}
logRetentionInDays: 1
timeout: 10
deploymentBucket: lambda-repository
memorySize: 128
tracing:
lambda: true
plugins:
- serverless-step-functions
configValidationMode: error
stepFunctions:
stateMachines:
sortData:
name: datasorting-dev
type: STANDARD
role: ${self:custom.datasorting.${self:provider.stage}.iam}
definition:
Comment: "Data Sort"
StartAt: Query Data
States:
Query Data:
Type: Task
Resource: arn:aws:states:::athena:startQueryExecution.sync
Parameters:
QueryString: >-
select * from table.data
WorkGroup: primary
ResultConfiguration:
OutputLocation: s3://output/location
Next: Insert Data Dynamodb
Insert Data Dynamodb:
Type: Task
Resource: arn:aws:states:::lambda:invoke
Parameters:
FunctionName: arn:aws:lambda:us-east-1:${account-id}:function:name
Payload:
OutputLocation.$: $.QueryExecution.ResultConfiguration.OutputLocation
Date.$: ${self:custom.dates.year}${self:custom.dates.month}${self:custom.dates.day}
End: true
Your Date.$ property has value of ${self:custom.dates.year}${self:custom.dates.month}${self:custom.dates.day}. Let's assume that:
const dates = {
"year": "2000",
"month": "01",
"day": "20"
}
The result will be Date.$: "20000120" which is not a valid JSON Path.
JSON Path needs to start with a $ sign and each level is divided by ..
Do you want to achieve something like this? $.2000.01.20?
As you see, the issue is not with passing 2 parameters but with wrong string JSON Path created by string interpolation for Date.$.
Some useful links:
https://github.com/json-path/JsonPath
https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-paths.html

Use a reference to the same object inside an object

I'm trying to do something like this:
User:
type: "object"
properties:
id:
type: "integer"
format: "int64"
name:
type: "string"
manager:
$ref: "/definitions/User"
Editor does not throw an exception, but the code generated according to this schema does not work at all.
I generated python-flask server, and on launch, it throws:
ImportError: cannot import name 'User'
Looking through the code I found that 'User' class uses 'User' keyword in __init__ and inside the class.
Also there was the import: from swagger_server.models.user import User
Does python-flask generator know how to implement references like this?

How to use "$ref" reference and declare components from Swagger in API platform?

We can define components in Swagger:
components:
schemas:
User:
properties:
id:
type: integer
name:
type: string
And use this component later:
responses:
'200':
description: The response
schema:
$ref: '#/components/schemas/User'
I want to use this in order to avoid duplicating content.
I try to use this syntax in API Platform:
components:
schemas:
Part:
description: Array of Part
type: array
items:
type: object
properties:
name:
type: string
App\Entity\Item:
collectionOperations:
post:
method: 'POST'
swagger_context:
parameters:
- name: body
description: Item data
in: body
schema:
type: object
properties:
name:
description: Part
type: string
required: true
part:
$ref: '#/components/schemas/Part'
It gives me an error:
Exception thrown when handling an exception (Symfony\Component\Config\Exception\FileLoaderLoadException: Resource "components" not found in . (which is being imported from "/app/config/routes/api_platform.yaml"). Make sure there is a loader supporting the "api_platform" type.)
It looks like the YAML loader doesn't recognize the components item.
How can I define and use references in API Platform? How can I define a reference and use it in several YAML files?
You cannot do that like this.
The components key belongs to the Swagger/OpenAPI format, not to the API Platform configuration (mapping) format. Both API Platform configuration files and Swagger definitions can be written in YAML, but they are not related.
So, as the error message describes, Swagger components cannot be injected in API Platform's configuration files directly, as you try to do.
API Platform's configuration does allow allows to inject some context in the generated Swagger file using the swagger_context key, but you cannot write random Swagger definitions (such as your component key) outside of this structure.
To do what you want to achieve the swagger_context key will not be enough (components must be injected at the root of the Swagger file, and it's not possible with swagger_context).
Instead of using this key, you'll have to create a decorator for the Swagger documentation generator, as explained in this documentation entry: https://api-platform.com/docs/core/swagger/#overriding-the-swagger-documentation
Decorator allows to access to the whole Swagger structure, and to modify it. So you'll be able to add your components structure.
It possible ... See How can I annotate my attribute which is Value Object in order that API Platform would generate its fields for swagger documentation?
In my exemple, I have the entity Checker, I created two groups :
* #ApiResource(
* attributes={
* "normalization_context"={"groups"={"read"}},
* "denormalization_context"={"groups"={"write"}},
* },
Then in the swagger_context responses :
* "responses" = {
* "201" = {
* "description" = "....",
* "schema" = {
* "type" = "object",
* "properties" = {
* "myresult" = {
* "$ref"="#/definitions/Checker-read"
* }

Polymorphism in Swagger produces strange error message on discriminator

I'm writing a service with a GET that can return one of five different but closely related types. Since the user wants the option of searching through all five types at once, it has to be a single get call. I'm returning JSON, which can easily handle any type.
I'm trying to do this in Swagger, using their polymorphism feature, which I've never tried before. I'm doing it just like in the example, except under "definitions" instead of "components/schemas". But I'm getting a strange error message that I can't understand. The swagger file is below. The error says this:
Schema error at definitions['Event'].discriminator should be string
It gives this on line 49, which says discriminator:
So, my two questions are: How can I fix it? And will this even give me what I need?
swagger: '2.0'
info:
description: RESTful API to retrieve Titles Metadata
version: 1.0.0
title: Swagger Mystery
schemes:
- https
paths:
/event:
get:
operationId: getEvent
summary: searches names
description: |
Search by names, across all types, or by a specific type.
produces:
- application/json
parameters:
- in: query
name: title
description: name to search for
required: true
type: string
- in: query
name: start
required: false
type: boolean
- in: query
name: type
required: false
type: string
description: |
May be "contest", "partner", "sponsor", or "dancer". If missing, will search for all types.
responses:
'200':
description: search results
# I also don't know why I need to comment these out.
# content:
# application/json:
# schema:
# type: array
# items:
# $ref: '#/definitions/Event'
'400':
description: bad input parameter
definitions:
Event:
type: object
discriminator:
propertyName: eventType
properties:
eventType:
type: string
id:
type: integer
format: int64
name:
type: string
description:
type: string
contests:
type: array
items:
$ref: '#/definitions/Contest'
required:
- id
- name
- description
- contests
- eventType
Contest:
allOf:
- $ref: '#/definitions/Event'
- type: object
properties:
parentEvent:
type: string
venue:
type: string
required:
- parentEvent
- venue
Dancer:
allOf:
- $ref: '#/definitions/Event'
- type: object
properties:
eventInvitationDate:
type: string
format: date
venue:
type: string
required:
- eventInvitationDate
- venue
# Sponsor:
# allOf:
# - $ref: '#/definitions/Event'
# - type: object
# properties:
# invitationDate:
# type: string
# format: date
# parentEvent:
# type: string
# partners:
# type: array
# items:
# $ref: '#/definitions/Partner'
Partner:
allOf:
- $ref: '#/definitions/Event'
- type: object
properties:
invitationDate:
type: string
format: date
parentEvent:
type: string
venue:
type: string
required:
- invitationDate
- parentEvent
- venue
# two problems:
# 1. Schema error at definitions['Event'].discriminator
# should be string on line 49 (discriminator:)
# 2. Resolver error:
# e is undefined
# (no line number)
# (This error goes away when I comment out Sponsor.)
The error occurs because you are mixing OpenAPI 2.0 and 3.0 syntax.
Your spec is swagger: '2.0' but the following is 3.0 syntax:
discriminator:
propertyName: eventType
In OpenAPI 2.0, the value of discriminator is the property name:
discriminator: eventType
Also, OpenAPI 2.0 assumes that the possible values of the discriminator property (in this case eventType) are exactly the same as the model names in definitions. That is:
If eventType can be event, partner etc. in lowercase, then the model names must also be in lowercase – event, not Event.
If eventType is some code like e, p, d etc., the model names must be e, p, d etc. instead of Event, Partner etc.
Check out questions for more examples of discriminator usage in OpenAPI 2.0:
Swagger Inheritance and Composition
“discriminator” in polymorphism, OpenAPI 2.0 (Swagger 2.0)
Swagger: variant schema shape dependant on field value

Swagger:Issue with Path parameter

I am try to create a swagger file with the following path:
paths:
/v1/customers/{id}/summary :
However I get the following error right off bat:
API requires path parameter but it is not defined: id
at paths ▹ /v1/customers/{id}/summary
It does not seem to like the 'id' parameter. Could anybody tell me how I could rectify this?
If I drill down on this I see the following:
Details
Object
swaggerError: Object
errors: Array [1]
0: Object
code: "MISSING_API_PATH_PARAMETER"
message: "API requires path parameter but it is not defined: id"
data: "/v1/customers/{id}/summary"
path: Array [2]
warnings: Array [0]
Basically, you're declaring a path that has a path parameter in it, by using path templates. In this case {id} declares a path parameter called id.
When you declare such a path, it means that you have to declare that path parameter as part of the operation.
Take a look at this YAML example:
/pets/{id}:
get:
description: Returns a user based on a single ID, if the user does not have access to the pet
operationId: findPetById
produces:
- application/json
- application/xml
- text/xml
- text/html
parameters:
- name: id
in: path
description: ID of pet to fetch
required: true
type: integer
format: int64
responses:
'200':
description: pet response
schema:
$ref: '#/definitions/pet'
default:
description: unexpected error
schema:
$ref: '#/definitions/errorModel'
You can see there's an {id} in the path, and a corresponding id parameter definition. Without it, the spec won't be valid.

Resources