Swagger code generator error with a reusable parameter - swagger

I have a following swagger file (parses fine with the Swagger online editor):
swagger: '2.0'
info:
title: Sample
version: 1.1.0
basePath: /base
paths:
/sample:
get:
summary: Gets the samples
parameters:
- $ref: '#parameters/reusableParam'
responses:
200:
description: Success
parameters:
reusableParam:
in: header
name: sample-param
required: true
type: string
description: Sample reusable parameter
When I run code generator on it:
java -jar swagger-codegen-cli-2.2.1.jar generate -i swagger.yaml -l java
It fails with an error:
Exception in thread "main" java.lang.NullPointerException
at io.swagger.codegen.DefaultGenerator.generateParameterId(DefaultGenerator.java:803)
at io.swagger.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:727)
at io.swagger.codegen.DefaultGenerator.processPaths(DefaultGenerator.java:688)
at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:376)
at io.swagger.codegen.cmd.Generate.run(Generate.java:223)
at io.swagger.codegen.SwaggerCodegen.main(SwaggerCodegen.java:36)
Am I doing something wrong in the definition of the reusable parameter?

There's a typo in your spec:
- $ref: '#parameters/reusableParam'
should be
- $ref: '#/parameters/reusableParam'

Related

How to write swagger yaml in swagger editor

Hi I wanted to create swagger opn api client code after writing swagger yaml on swagger editor, When I am trying to write it is throwing several errors again and again.
openapi: 3.0.0
info:
description: |
Rest Server API .
version: 1.0.0-oas3
title: Implementation of GET API in Swagger
paths:
/details/:
get:
parameters:
- name: details
in: path
schema:
type: string
enum: ['all', 'message', 'successfulCalls','failedCalls']
default: all
required: true
definitions:
details:
type: object
properties:
details_name_test:
type: array
items:
$ref: '#/definitions/Call'
Call:
type: int
responses:
'200':
description: A list of calls (maybe filtered by details)
schema:
$ref: '#/definitions/details'
properties:
message:
type: string
'400':
description: 'Invalid request'
schema:
$ref: '#/definitions/details'
properties:
message:
type: string
Can someone please help me to fix it?
If you use openapi-generator, you can validate your openapi file before generating.
usage: openapi-generator-cli <command> [<args>]
The most commonly used openapi-generator-cli commands are:
author Utilities for authoring generators or customizing templates.
batch Generate code in batch via external configs.
config-help Config help for chosen lang
generate Generate code with the specified generator.
help Display help information about openapi-generator
list Lists the available generators
meta MetaGenerator. Generator for creating a new template set and configuration for Codegen. The output will be based on the language you specify, and includes default templates to include.
validate Validate specification
version Show version information used in tooling
See 'openapi-generator-cli help <command>' for more information on a specific command.
java -jar openapi-generator-cli-6.0.0.jar validate -i <your_openapi_file.yaml>
If your schema is valid, you can generate the code with jar file or docker.
Docker example:
docker run --rm \
-v ${PWD}:/local openapitools/openapi-generator-cli generate \
-i /local/<your_openapi_file.yaml> \
-g <choose_generator> \
-o /local/generated/

Swagger 2.0 - attribute responses.responses.bad$ref is not of type `object`

I am using Swagger 2.0.
Facing the error on UI as:
{"messages":["attribute responses.responses.bad$ref is not of type object"],"schemaValidationMessages":[{"level":"error","domain":"validation","keyword":"oneOf","message":"instance failed to match exactly one schema (matched 0 out of 2)","schema":{"loadingURI":"http://swagger.io/v2/schema.json#","pointer":"/definitions/parameter"},"instance":{"pointer":"/parameters/bad$ref"}}]}
It's a YAML based documentation with the following file referencing in the main swagger.yaml.
swagger: '2.0'
info:
$ref: ./info/index.yaml
host: example.net
basePath: /api/v1
tags:
- name: Name
description: description
schemes:
- http
- https
produces:
- application/json
consumes:
- application/json
parameters:
$ref: ./parameters/index.yaml
responses:
$ref: ./responses/index.yaml
paths:
$ref: ./paths/index.yaml
definitions:
$ref: ./definitions/index.yaml
externalDocs:
description: Find out more about Swagger
url: 'http://swagger.io'
Folder structure:
The parameters/index.yaml and responses/index.xml files are empty. This works perfectly in the Swagger Editor.

Adding schema definition in openapi3.0 yaml using external .yaml file

I've openapi3.0 YAML file which is written according to openapi3.0 format and I am using $ swagger-cli validate simple_violation_bool.yaml and its gave True/False based on whether simple_violation_bool.yaml is valid OpenAPI 3.0 or not.
Below is the content of my OpenAPI3.0 yaml file i.e. simple_violation_bool.yaml and I am trying to add schema definition using $ref: './violation_schema.yaml#/NISE but it giving error during $ swagger-cli validate simple_violation_bool.yaml.
Below is my simple_violation_bool.yaml openapi3.0 YAML file.
simple_violation_bool.yaml
openapi: "3.0.0"
info:
version: 1.0.0
title: simple_violation_bool
license:
name: MIT
description: |
Simple violation in simple_violation_bool module
externalDocs:
description: NISE simple_violation_bool.
servers:
- url: https://swagger.io/specification/
paths: {}
components:
schemas:
NISE:
type: object
title: The Root Schema
required:
- description
- id
- name
- ports
properties:
description:
type: string
title: The Descripton Schema
schema:
$ref: './violation_schema.yaml#/NISE'
id:
type: integer
title: The Id Schema
default: 0
format: int64
schema:
$ref: './violation_schema.yaml#/NISE'
name:
type: string
title: The Name Schema
schema:
$ref: './violation_schema.yaml#/NISE'
ports:
type: array
title: The Ports Schema
schema:
$ref: './violation_schema.yaml#/NISE'
items:
type: integer
title: The items Schema
default: 0
schema:
$ref: './violation_schema.yaml#/NISE'
value:
type: object
title: The Value Schema
schema:
$ref: './violation_schema.yaml#/NISE'
And here is the content of schema definition file violation_schema.yaml which I am trying to add using $ref: './violation_schema.yaml.
violation_schema.yaml
NISE:
properties:
description:
type: string
id:
type: integer
name:
type: string
ports:
type: array
value:
type: object
Below is the error log after I run $ swagger-cli validate simple_violation_bool.yaml
Error Log:
Running swagger-cli validate on simple_violation_bool.yaml .....
Swagger schema validation failed.
Data does not match any schemas from 'oneOf' at #/components/schemas/NISE
Data does not match any schemas from 'oneOf' at #/components/schemas/NISE/properties/value
Additional properties not allowed: schema at #/properties/value
Missing required property: $ref at #/properties/value
Missing required property: $ref at #/components/schemas/NISE
JSON_OBJECT_VALIDATION_FAILED
Any insight on this issue?
Lastly, I've tried with some simple openapi3.0 yaml files in the same way and it's working without any issue.
Examples which I've tried
main.yaml
openapi: 3.0.1
info:
title: ping test
version: '1.0'
servers:
- url: 'http://localhost:8000/'
paths:
/some/ping:
get:
operationId: pingGet
responses:
'201':
description: OK
content:
application/json:
schema:
$ref: './other.yaml#/SomeObj'
components:
schemas: {}
other.yaml
SomeObj:
properties:
s1:
type: string
s3:
type: string
This simple example works without issue. i.e $ swagger-cli validate main.yaml outputs: main.yaml is valid
Looking forward to hearing soon & thanks for spending time to look into this descriptive question.
Arslan
The way in which you are trying to refer to the schema defined in the violation_schema.yaml from simple_violation_bool.yaml is not correct . You do not need to define $ref: './violation_schema.yaml#/NISE' line at against each property defined in the simple_violation_bool.yaml file . Also , since you are defining schema altogether in a different schema file violation_schema.yaml , you do not need to define all those values again in the simple_violation_bool.yaml,i.e id,name,ports,value,etc.. Let's try to understand in a simple way , lets say you were not defining a separate file for the schema, but referring it from within the same file , i.e simple_violation_bool.yaml, then in that case things will look something like this as below :
So ,based on below observation , i corrected your yaml files and now its validating properly .
simple_violation_bool.yaml
openapi: "3.0.0"
info:
version: 1.0.0
title: simple_violation_bool
license:
name: MIT
description: |
Simple violation in simple_violation_bool module
externalDocs:
description: NISE simple_violation_bool.
url: "https://simple_violation_bool.net"
servers:
- url: https://swagger.io/specification/
paths: {}
components:
schemas:
ROOT:
type: object
title: The Root Schema
$ref: 'violation_schema.yaml#/NISE'
violation_schema.yaml
NISE:
required:
- description
- id
- name
- ports
properties:
description:
type: string
title: The Descripton Schema
id:
type: integer
title: The Id Schema
default: 0
format: int64
name:
type: string
title: The Name Schema
ports:
type: array
title: The Ports Schema
items:
type: integer
title: The items Schema
default: 0
value:
type: object
title: The Value Schema
swagger-cli validate simple_violation_bool.yaml
simple_violation_bool.yaml is valid
I hope that it resolves the issue for you . Let me know if that helps !!

Using OpenAPI3 (swagger) in multiple microservices and generating a single OpenAPI definition

I have a collection of microservices, each with their own complete OpenAPI spec and a root level path
Microservice Definition
openapi: 3.0.0
info:
description: Microservice API
version: '0.0.1'
paths:
/:
get:
tags:
- Root
summary: Root Path
operationId: Root
responses:
'200':
description: Request was successful
deprecated: false
/healthcheck/ping:
get:
tags:
- Healthcheck
summary: Test the reachability of the microservice.
operationId: Ping
responses:
'200':
description: Request was successful
deprecated: false
And a root OpenAPI definition
openapi: "3.0.0"
info:
version: 1.0.0
title: API
servers:
- url: http://api.something.com/v1
paths:
/microservice1:
$ref: "https://storagebucket.someservice.com/service-definitions/microservice-swagger.yaml"
It's been my assumption that it's possible to arrange things this way but I'm getting a whole host of errors in the swagger editor for validity.
Using microservice-swagger.yaml#/paths/~1 gets me somewhere but I would expect that all the paths in the service yaml to simply be appended to the path in the main definition. Does anyone know how to achieve this?
(The reason behind it being to apply this root swagger to an API Gateway whilst maintaining each service as a self contained unit)
I would for example expect the resulting swagger to offer this as a path
/microservice1/healthcheck/ping

Swagger fake API with swagger-node

README.md on https://github.com/swagger-api/swagger-node says 'Kick the tires. Your API is live while you edit (Did we mention no code?)' -> 'Quit faking' on the next step.
So I was under impression that Swagger will generate a fake API for me.
However, if I use such swagger.yaml:
swagger: '2.0'
info:
title: test
description: test
version: "1.0.0"
host: localhost:10010
schemes:
- https
basePath: /api
produces:
- application/json
- text/event-stream
consumes:
- application/json
paths:
/pages:
get:
summary: test
description: test
responses:
200:
description: pages
schema:
type: array
items:
$ref: '#/definitions/Page'
default:
description: Unexpected error
schema:
$ref: '#/definitions/Error'
(where definitions are also given in config)
I receive 404 in both curl http://localhost:10010/api/pages and swagger editor (swagger project edit). I know about x-swagger-router-controller thing but I expected it to work out of the box. Am I doing something wrong?
I should've read the docs more carefully. Swagger gives us the mock mode (swagger project start -m)

Resources