use of io.swagger.v3.oas.models.info.Info in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testExampleVsExamples.
@Test
public void testExampleVsExamples() {
String json = "{" + "\"openapi\": \"3.0.0\"," + "\"info\": {\"title\": \"Examples\", \"version\": \"0.0.0\"}," + "\"paths\": {}," + "\"components\": {" + " \"parameters\": {" + " \"withExample\": {" + " \"name\": \"withExample\"," + " \"in\": \"query\"," + " \"schema\": {\"type\": \"string\"}," + " \"example\": \"Hello\"}," + " \"withExamples\": {" + " \"name\": \"withExamples\"," + " \"in\": \"query\"," + " \"schema\": {\"type\": \"string\"}," + " \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}," + " \"withBoth\": {" + " \"name\": \"withBoth\"," + " \"in\": \"query\"," + " \"schema\": {\"type\": \"string\"}," + " \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}," + " \"example\": \"Hello\"}," + " \"withContentExample\": {" + " \"name\": \"withContentExample\"," + " \"in\": \"query\"," + " \"content\": {" + " \"application/json\": {" + " \"schema\": {\"type\": \"string\"}," + " \"example\": \"Hello\"}}}," + " \"withContentExamples\": {" + " \"name\": \"withContentExamples\"," + " \"in\": \"query\"," + " \"content\": {" + " \"application/json\": {" + " \"schema\": {\"type\": \"string\"}," + " \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}}}," + " \"withContentBoth\": {" + " \"name\": \"withContentBoth\"," + " \"in\": \"query\"," + " \"content\": {" + " \"application/json\": {" + " \"schema\": {\"type\": \"string\"}," + " \"example\": \"Hello\"," + " \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}}}}," + " \"headers\": {" + " \"withExample\": {" + " \"schema\": {\"type\": \"string\"}," + " \"example\": \"Hello\"}," + " \"withExamples\": {" + " \"schema\": {\"type\": \"string\"}," + " \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}," + " \"withBoth\": {" + " \"schema\": {\"type\": \"string\"}," + " \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}," + " \"example\": \"Hello\"}}," + " \"requestBodies\": {" + " \"withBodyExample\": {" + " \"content\": {" + " \"application/json\": {" + " \"schema\": {\"type\": \"string\"}," + " \"example\": \"Hello\"}}}," + " \"withBodyExamples\": {" + " \"content\": {" + " \"application/json\": {" + " \"schema\": {\"type\": \"string\"}," + " \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}}}," + " \"withBodyBoth\": {" + " \"content\": {" + " \"application/json\": {" + " \"schema\": {\"type\": \"string\"}," + " \"example\": \"Hello\"," + " \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}}}}}}";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
SwaggerParseResult result = parser.readContents(json, null, null);
assertEqualsNoOrder(result.getMessages().toArray(), new Object[] { "attribute components.parameters.withBoth.[withBoth].examples already defined -- ignoring \"example\" field", "attribute components.parameters.withContentBoth.[withContentBoth].content.'application/json'.examples already defined -- ignoring \"example\" field", "attribute components.requestBodies.withBodyBoth.content.'application/json'.examples already defined -- ignoring \"example\" field", "attribute components.headers.withBoth.examples already defined -- ignoring \"example\" field" }, "Expected warnings not found");
OpenAPI openAPI = result.getOpenAPI();
Parameter param;
param = openAPI.getComponents().getParameters().get("withExample");
assertNull(param.getExamples(), "Examples,");
assertNotNull(param.getExample(), "Example,");
param = openAPI.getComponents().getParameters().get("withExamples");
assertNotNull(param.getExamples(), "Examples,");
assertNull(param.getExample(), "Example,");
param = openAPI.getComponents().getParameters().get("withBoth");
assertNotNull(param.getExamples(), "Examples,");
assertNull(param.getExample(), "Example,");
Header header;
header = openAPI.getComponents().getHeaders().get("withExample");
assertNull(header.getExamples(), "Examples,");
assertNotNull(header.getExample(), "Example,");
header = openAPI.getComponents().getHeaders().get("withExamples");
assertNotNull(header.getExamples(), "Examples,");
assertNull(header.getExample(), "Example,");
header = openAPI.getComponents().getHeaders().get("withBoth");
assertNotNull(header.getExamples(), "Examples,");
assertNull(header.getExample(), "Example,");
MediaType mediaType;
mediaType = openAPI.getComponents().getParameters().get("withContentExample").getContent().get("application/json");
assertNull(mediaType.getExamples(), "Examples,");
assertNotNull(mediaType.getExample(), "Example,");
mediaType = openAPI.getComponents().getParameters().get("withContentExamples").getContent().get("application/json");
assertNotNull(mediaType.getExamples(), "Examples,");
assertNull(mediaType.getExample(), "Example,");
mediaType = openAPI.getComponents().getParameters().get("withContentBoth").getContent().get("application/json");
assertNotNull(mediaType.getExamples(), "Examples,");
assertNull(mediaType.getExample(), "Example,");
mediaType = openAPI.getComponents().getRequestBodies().get("withBodyExample").getContent().get("application/json");
assertNull(mediaType.getExamples(), "Examples,");
assertNotNull(mediaType.getExample(), "Example,");
mediaType = openAPI.getComponents().getRequestBodies().get("withBodyExamples").getContent().get("application/json");
assertNotNull(mediaType.getExamples(), "Examples,");
assertNull(mediaType.getExample(), "Example,");
mediaType = openAPI.getComponents().getRequestBodies().get("withBodyBoth").getContent().get("application/json");
assertNotNull(mediaType.getExamples(), "Examples,");
assertNull(mediaType.getExample(), "Example,");
}
use of io.swagger.v3.oas.models.info.Info in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testExamples.
@Test
public void testExamples(@Injectable List<AuthorizationValue> auths) {
String yaml = "openapi: 3.0.1\n" + "info:\n" + " title: httpbin\n" + " version: 0.0.0\n" + "servers:\n" + " - url: http://httpbin.org\n" + "paths:\n" + " /post:\n" + " post:\n" + " summary: Returns the POSTed data\n" + " requestBody:\n" + " content:\n" + " application/json:\n" + " schema:\n" + " $ref: '#/components/schemas/AnyValue'\n" + " examples:\n" + " AnObject:\n" + " $ref: '#/components/examples/AnObject'\n" + " ANull:\n" + " $ref: '#/components/examples/ANull'\n" + " application/yaml:\n" + " schema:\n" + " $ref: '#/components/schemas/AnyValue'\n" + " examples:\n" + " AString:\n" + " $ref: '#/components/examples/AString'\n" + " AnArray:\n" + " $ref: '#/components/examples/AnArray'\n" + " text/plain:\n" + " schema:\n" + " type: string\n" + " example: Hi there\n" + " application/x-www-form-urlencoded:\n" + " schema:\n" + " type: object\n" + " properties:\n" + " id:\n" + " type: integer\n" + " name:\n" + " type: string\n" + " example:\n" + " id: 42\n" + " name: Arthur Dent\n" + " responses:\n" + " '200':\n" + " description: OK\n" + " content:\n" + " application/json:\n" + " schema:\n" + " type: object\n" + "\n" + " #/response-headers:\n" + " /:\n" + " get:\n" + " summary: Returns a response with the specified headers\n" + " parameters:\n" + " - in: header\n" + " name: Server\n" + " required: true\n" + " schema:\n" + " type: string\n" + " examples:\n" + " httpbin:\n" + " value: httpbin\n" + " unicorn:\n" + " value: unicorn\n" + " - in: header\n" + " name: X-Request-Id\n" + " required: true\n" + " schema:\n" + " type: integer\n" + " example: 37\n" + " responses:\n" + " '200':\n" + " description: A response with the specified headers\n" + " headers:\n" + " Server:\n" + " schema:\n" + " type: string\n" + " examples:\n" + " httpbin:\n" + " value: httpbin\n" + " unicorn:\n" + " value: unicorn\n" + " X-Request-Id:\n" + " schema:\n" + " type: integer\n" + " example: 37\n" + "\n" + "components:\n" + " schemas:\n" + " AnyValue:\n" + " nullable: true\n" + " description: Can be anything - string, object, array, null, etc.\n" + "\n" + " examples:\n" + " AString:\n" + " value: Hi there\n" + " ANumber:\n" + " value: 42\n" + " ANull:\n" + " value: null\n" + " AnArray:\n" + " value: [1, 2, 3]\n" + " AnObject:\n" + " value:\n" + " id: 42\n" + " name: Arthur Dent";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
ParseOptions options = new ParseOptions();
options.setResolve(true);
SwaggerParseResult result = parser.readContents(yaml, auths, options);
OpenAPI openAPI = result.getOpenAPI();
MediaType mediaTypeJson = openAPI.getPaths().get("/post").getPost().getRequestBody().getContent().get("application/json");
Header header1 = openAPI.getPaths().get("/").getGet().getResponses().get("200").getHeaders().get("Server");
Header header2 = openAPI.getPaths().get("/").getGet().getResponses().get("200").getHeaders().get("X-Request-Id");
Parameter parameter1 = openAPI.getPaths().get("/").getGet().getParameters().get(0);
Parameter parameter2 = openAPI.getPaths().get("/").getGet().getParameters().get(1);
Assert.assertNotNull(mediaTypeJson.getExamples());
Assert.assertEquals(mediaTypeJson.getExamples().get("AnObject").get$ref(), "#/components/examples/AnObject");
Assert.assertNotNull(header1.getExamples());
Assert.assertEquals(header1.getExamples().get("httpbin").getValue(), "httpbin");
Assert.assertNotNull(header2.getExample());
Assert.assertEquals(header2.getExample(), 37);
Assert.assertNotNull(parameter1.getExamples());
Assert.assertEquals(parameter1.getExamples().get("unicorn").getValue(), "unicorn");
Assert.assertNotNull(parameter2.getExample());
Assert.assertEquals(parameter2.getExample(), 37);
}
use of io.swagger.v3.oas.models.info.Info in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testDeserializeEnum.
@Test
public void testDeserializeEnum() {
String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + " version: 0.0.0\n" + " title: your title\n" + "paths:\n" + " /persons:\n" + " get:\n" + " description: a test\n" + " responses:\n" + " '200':\n" + " description: Successful response\n" + " content:\n" + " '*/*':\n" + " schema:\n" + " type: object\n" + " properties:\n" + " se:\n" + " $ref: '#/components/schemas/StringEnum'\n" + " ie:\n" + " $ref: '#/components/schemas/IntegerEnum'\n" + " ne:\n" + " $ref: '#/components/schemas/NumberEnum'\n" + " be:\n" + " $ref: '#/components/schemas/BooleanEnum'\n" + " ae:\n" + " $ref: '#/components/schemas/ArrayEnum'\n" + " oe:\n" + " $ref: '#/components/schemas/ObjectEnum'\n" + "components:\n" + " schemas:\n" + " StringEnum:\n" + " type: string\n" + " default: foo\n" + " enum:\n" + " - First\n" + " - Second\n" + " BooleanEnum:\n" + " enum:\n" + " - true \n" + " - false \n" + " IntegerEnum:\n" + " type: integer\n" + " default: 1\n" + " enum:\n" + " - -1\n" + " - 0\n" + " - 1\n" + " NumberEnum:\n" + " type: number\n" + " default: 3.14\n" + " enum:\n" + " - -1.151\n" + " - 0\n" + " - 1.6161\n" + " - 3.14\n" + " ArrayEnum:\n" + " type: array\n" + " items:\n" + " type: string\n" + " enum:\n" + " - - Camry\n" + " - Prius\n" + " - null\n" + " - - Pilot\n" + " - Passport\n" + " - - Rogue\n" + " - Leaf\n" + " ObjectEnum:\n" + " type: object\n" + " enum:\n" + " - make: Toyota\n" + " model: Prius\n" + " - make: Honda\n" + " model: Pilot\n" + " - make: Nissan\n" + " model: Leaf\n" + " - null\n";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
SwaggerParseResult result = parser.readContents(yaml, null, null);
final OpenAPI resolved = new OpenAPIResolver(result.getOpenAPI(), null).resolve();
Schema stringModel = resolved.getComponents().getSchemas().get("StringEnum");
assertTrue(stringModel instanceof Schema);
Schema stringImpl = stringModel;
List<String> stringValues = stringImpl.getEnum();
assertEquals(2, stringValues.size());
assertEquals("First", stringValues.get(0));
assertEquals("Second", stringValues.get(1));
Schema integerModel = resolved.getComponents().getSchemas().get("IntegerEnum");
assertTrue(integerModel instanceof Schema);
Schema integerImpl = integerModel;
List<String> integerValues = integerImpl.getEnum();
assertEquals(3, integerValues.size());
assertEquals(-1, integerValues.get(0));
assertEquals(0, integerValues.get(1));
assertEquals(1, integerValues.get(2));
assertEquals(integerImpl.getDefault(), 1);
Schema numberModel = resolved.getComponents().getSchemas().get("NumberEnum");
assertTrue(numberModel instanceof Schema);
Schema numberImpl = numberModel;
List<String> numberValues = numberImpl.getEnum();
assertEquals(4, numberValues.size());
assertEquals(new BigDecimal("-1.151"), numberValues.get(0));
assertEquals(new BigDecimal("0"), numberValues.get(1));
assertEquals(new BigDecimal("1.6161"), numberValues.get(2));
assertEquals(new BigDecimal("3.14"), numberValues.get(3));
assertEquals(numberImpl.getDefault(), new BigDecimal("3.14"));
Schema booleanModel = resolved.getComponents().getSchemas().get("BooleanEnum");
assertEquals("boolean", booleanModel.getType());
List<Object> booleanValues = booleanModel.getEnum();
assertEquals(2, booleanValues.size());
assertEquals(Boolean.TRUE, booleanValues.get(0));
assertEquals(Boolean.FALSE, booleanValues.get(1));
Schema arrayModel = resolved.getComponents().getSchemas().get("ArrayEnum");
assertEquals("array", arrayModel.getType());
List<Object> arrayValues = arrayModel.getEnum();
assertEquals(arrayValues.size(), 4);
assertEquals(arrayValues.get(0), JsonNodeFactory.instance.arrayNode().add("Camry").add("Prius"));
assertEquals(arrayValues.get(1), null);
assertEquals(arrayValues.get(2), JsonNodeFactory.instance.arrayNode().add("Pilot").add("Passport"));
assertEquals(arrayValues.get(3), JsonNodeFactory.instance.arrayNode().add("Rogue").add("Leaf"));
Schema objectModel = resolved.getComponents().getSchemas().get("ObjectEnum");
assertEquals("object", objectModel.getType());
List<Object> objectValues = objectModel.getEnum();
assertEquals(objectValues.size(), 4);
assertEquals(objectValues.get(0), JsonNodeFactory.instance.objectNode().put("make", "Toyota").put("model", "Prius"));
assertEquals(objectValues.get(1), JsonNodeFactory.instance.objectNode().put("make", "Honda").put("model", "Pilot"));
assertEquals(objectValues.get(2), JsonNodeFactory.instance.objectNode().put("make", "Nissan").put("model", "Leaf"));
assertEquals(objectValues.get(3), null);
}
use of io.swagger.v3.oas.models.info.Info in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testArrayQueryParam.
@Test
public void testArrayQueryParam() throws Exception {
String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "paths:\n" + " /pet/findByStatus:\n" + " get:\n" + " parameters:\n" + " - name: status\n" + " in: query\n" + " description: Status values that need to be considered for filter\n" + " required: false\n" + " style: pipeDelimited\n" + " schema:\n" + " type: array\n" + " items:\n" + " type: string\n" + " default: available\n" + " responses:\n" + " '200':\n" + " description: successful operation\n" + " content:\n" + " '*/*':\n" + " schema:\n" + " $ref: #/components/schemas/PetArray\n" + "info:\n" + " version: ''\n" + " title: ''";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
SwaggerParseResult result = parser.readContents(yaml, null, null);
OpenAPI openAPI = result.getOpenAPI();
Parameter param = openAPI.getPaths().get("/pet/findByStatus").getGet().getParameters().get(0);
assertTrue(param instanceof QueryParameter);
QueryParameter qp = (QueryParameter) param;
Schema p = qp.getSchema();
assertEquals(p.getType(), "array");
assertTrue(((ArraySchema) p).getItems() instanceof StringSchema);
}
use of io.swagger.v3.oas.models.info.Info in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testNestedDefinitions.
@Test
public void testNestedDefinitions() {
String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + " version: ''\n" + " title: ''\n" + "paths: {}\n" + "components:\n" + " schemas:\n" + " Person:\n" + " required:\n" + " - id\n" + " - name\n" + " properties:\n" + " id:\n" + " type: integer\n" + " format: int64\n" + " name:\n" + " type: string\n" + " address:\n" + " $ref: '#/components/schemas/Address'\n" + " Address:\n" + " required:\n" + " - zip\n" + " properties:\n" + " street:\n" + " type: string\n" + " zip:\n" + " type: integer\n" + " format: int32\n" + " minimum: 0\n" + " exclusiveMinimum: true\n" + " maximum: 99999\n" + " exclusiveMaximum: true";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
SwaggerParseResult result = parser.readContents(yaml, null, null);
assertTrue(result.getOpenAPI().getComponents().getSchemas().get("Person") instanceof Schema);
assertTrue(result.getOpenAPI().getComponents().getSchemas().get("Address") instanceof Schema);
Schema person = result.getOpenAPI().getComponents().getSchemas().get("Person");
Schema property = (Schema) person.getProperties().get("address");
assertTrue(property.get$ref() != null);
Schema zip = (Schema) (result.getOpenAPI().getComponents().getSchemas().get("Address")).getProperties().get("zip");
assertTrue(zip instanceof IntegerSchema);
IntegerSchema zipProperty = (IntegerSchema) zip;
assertEquals(zipProperty.getMinimum(), new BigDecimal("0"));
assertTrue(zipProperty.getExclusiveMinimum());
assertEquals(zipProperty.getMaximum(), new BigDecimal("99999"));
assertTrue(zipProperty.getExclusiveMaximum());
}
Aggregations