How to write swagger yaml in swagger editor - swagger

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/

Related

swagger-codegen is not correctly generating common parameters for any language

According to the swagger documentation, I should be able to have common parameters that are shared by all operations. The problem is that when running codegen locally, the generated code does not have any of the common path parameters. The below yaml produces code for any language (I've tried two).
What is very confusing is that if I use this exact yaml in https://editor.swagger.io/, the generated code does have the path parameters. I ran this for two different languages, typescript:
And C#:
Left is the code generated in editor.swagger.io and right is my generated code by running codegen locally.
In both cases, the .swagger-codegen\VERSION the file is 3.0.20 which is the one I'm using but the code generated by https://editor.swagger.io/ does have the parameter paths.
This simple yaml file reproduces the issue:
openapi: 3.0.2
info:
title: title
version: 1.0.0
paths:
'/instances/{id}':
summary: Manipulate a particular instance
get:
responses:
'200':
description: Ok
content:
text/plain:
schema:
type: string
example: pong
summary: Fetches an instance
parameters:
- in: path
name: id
schema:
type: integer
required: true
components:
securitySchemes:
bearerAuth:
scheme: bearer
bearerFormat: JWT
type: http
The command line used for generation:
java ^
-classpath bin/swagger-codegen-cli.jar ^
-DdebugOperations ^
io.swagger.codegen.v3.Codegen ^
generate ^
-i enterpos-api.yaml ^
-l typescript-angular ^
-o generated-code/typescript-angular-builtin
And that generated this output: https://gist.github.com/alanboy/45ce792255e079dd0de4f70449ebf455. I feel like this might be wrong usage or something wrong with my yaml but I can't figure out what.
The problem is the Main class. I noticed that using the io.swagger.codegen.v3.Codegen class produces incorrect results :
java -classpath bin/swagger-codegen-cli.jar io.swagger.codegen.v3.Codegen <more>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
But this works:
java -jar bin/swagger-codegen-cli.jar <more>
Which led me to open MANIFEST.MF in the jar and notice the main class is actually this:
Main-Class: io.swagger.codegen.v3.cli.SwaggerCodegen
I then ran the command like this and everything worked as expected.
java -classpath bin/swagger-codegen-cli.jar io.swagger.codegen.v3.cli.SwaggerCodegen <more>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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 code generator error with a reusable parameter

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'

How to generate models that have a variable whose data type is Currency(java.util) with Swagger Codegen

Is there a way to generate models that have a variable whose data type is Currency(java.util) with Swagger Codegen?
Note: I use Swagger Version 2.0 and Swagger Codegen Version 2.2.3
You can define a Currency object in your spec and then use --import-mappings to avoid creating a model for it.
The (partial) spec:
definitions:
Bill:
type: "object"
properties:
id:
type: "integer"
format: "int64"
category:
$ref: "#/definitions/Currency"
Currency:
type: "object"
The command:
java -jar swagger-codegen-cli.jar generate -l java -i MySpec.yaml --import-mappings Currency=java.util.Currency
Or if you are using Maven, add this to the pom.xml:
<configOptions>
<import-mappings>Currency=java.util.Currency</import-mapping‌​s>
</configOptions>

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