Swagger API Tools: Difference between revisions
Jump to navigation
Jump to search
(14 intermediate revisions by the same user not shown) | |||
Line 12: | Line 12: | ||
{{External|OpenAPI 3.0 implementation https://github.com/swagger-api/swagger-core}} | {{External|OpenAPI 3.0 implementation https://github.com/swagger-api/swagger-core}} | ||
{{External|OpenAPI 2.0 implementation https://github.com/swagger-api/swagger-core/tree/1.5}} | {{External|OpenAPI 2.0 implementation https://github.com/swagger-api/swagger-core/tree/1.5}} | ||
==Swagger Core Playground Example== | |||
{{External|[https://github.com/ovidiuf/playground/tree/master/swagger/swagger-parser-example Playground swagger-core Example]}} | |||
==Parsing Swagger== | |||
<syntaxhighlight lang='java'> | |||
import io.swagger.models.Swagger; | |||
import io.swagger.parser.SwaggerParser; | |||
... | |||
String filePath = ...; | |||
final SwaggerParser parser = new SwaggerParser(); | |||
Swagger swagger = parser.read(filePath); | |||
</syntaxhighlight> | |||
==Render Swagger as Yaml== | |||
{{External|[https://github.com/ovidiuf/playground/blob/b5708d871c345934d890c1b17ca8a58bd8726dc7/swagger/swagger-parser-example/src/main/java/playground/swagger/YamlExamples.java#L11-L14 Render Swagger as Yaml]}} | |||
<syntaxhighlight lang='java'> | |||
import io.swagger.models.Swagger; | |||
import io.swagger.util.Yaml; | |||
Swagger swagger = ... | |||
String s = Yaml.mapper().writeValueAsString(swagger); | |||
</syntaxhighlight> | |||
Alternative: | |||
<syntaxhighlight lang='java'> | |||
String s = Yaml.pretty(swagger); | |||
</syntaxhighlight> | |||
==Render Swagger as Json== | |||
{{External|[https://github.com/ovidiuf/playground/blob/2e9dc3c5df32c7e1daee47610f264866b93c57f1/swagger/swagger-parser-example/src/main/java/playground/swagger/JsonExamples.java#L12-L15 Render Swagger as Json]}} | |||
<syntaxhighlight lang='java'> | |||
import io.swagger.models.Swagger; | |||
import io.swagger.util.Json; | |||
Swagger swagger = ... | |||
String s = Json.mapper().writeValueAsString(swagger); | |||
</syntaxhighlight> | |||
Alternative: | |||
<syntaxhighlight lang='java'> | |||
String s = Json.pretty(swagger); | |||
</syntaxhighlight> | |||
=Swagger Parser= | =Swagger Parser= | ||
Line 24: | Line 76: | ||
==Swagger Parser Playground Example== | ==Swagger Parser Playground Example== | ||
{{External|}} | {{External|[https://github.com/ovidiuf/playground/tree/master/swagger/swagger-parser-example Playground swagger-parser Example]}} | ||
==Dependencies== | ==Dependencies== |
Latest revision as of 01:52, 29 March 2019
External
Internal
Swagger Core
- OpenAPI 3.0 implementation https://github.com/swagger-api/swagger-core
- OpenAPI 2.0 implementation https://github.com/swagger-api/swagger-core/tree/1.5
Swagger Core Playground Example
Parsing Swagger
import io.swagger.models.Swagger;
import io.swagger.parser.SwaggerParser;
...
String filePath = ...;
final SwaggerParser parser = new SwaggerParser();
Swagger swagger = parser.read(filePath);
Render Swagger as Yaml
import io.swagger.models.Swagger;
import io.swagger.util.Yaml;
Swagger swagger = ...
String s = Yaml.mapper().writeValueAsString(swagger);
Alternative:
String s = Yaml.pretty(swagger);
Render Swagger as Json
import io.swagger.models.Swagger;
import io.swagger.util.Json;
Swagger swagger = ...
String s = Json.mapper().writeValueAsString(swagger);
Alternative:
String s = Json.pretty(swagger);
Swagger Parser
- Swagger Parser 1.X (OpenAPI 2.0) https://github.com/swagger-api/swagger-parser/tree/v1
- Swagger Parser 2.X (OpenAPI 3.0) https://github.com/swagger-api/swagger-parser
Swagger Parser Overview
Swagger Parser dependes on Swagger Core.
Swagger Parser Playground Example
Dependencies
dependencies {
implementation "io.swagger:swagger-parser:1.0.42"
implementation "io.swagger:swagger-parser:1.0.42:sources"
}