use of io.swagger.v3.parser.OpenAPIV3Parser in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testOneOfSchema.
@Test
public void testOneOfSchema(@Injectable List<AuthorizationValue> auths) {
String yaml = "openapi: '3.0'\n" + "components:\n" + " schemas:\n" + " Cat:\n" + " type: object\n" + " # all properties specific to a `Cat`\n" + " properties:\n" + " purring:\n" + " type: string\n" + " Dog:\n" + " type: object\n" + " # all properties specific to a `Dog`\n" + " properties:\n" + " bark:\n" + " type: string\n" + " Pet:\n" + " oneOf: \n" + " - $ref: '#/components/schemas/Cat'\n" + " - $ref: '#/components/schemas/Dog'\n" + " - type: object\n" + " # neither a `Cat` nor a `Dog`\n" + " properties:\n" + " name:\n" + " type: string\n";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
ParseOptions options = new ParseOptions();
options.setResolve(true);
SwaggerParseResult result = parser.readContents(yaml, auths, options);
List<String> messageList = result.getMessages();
Set<String> messages = new HashSet<>(messageList);
Schema petSchema = result.getOpenAPI().getComponents().getSchemas().get("Pet");
assertTrue(petSchema != null);
assertTrue(petSchema instanceof ComposedSchema);
ComposedSchema petCompSchema = (ComposedSchema) petSchema;
List<Schema> oneOfSchemas = petCompSchema.getOneOf();
assertTrue(oneOfSchemas != null);
assertEquals(oneOfSchemas.size(), 3);
Schema refCatSchema = oneOfSchemas.get(0);
assertTrue(refCatSchema != null);
assertEquals(refCatSchema.get$ref(), "#/components/schemas/Cat");
Schema refDogSchema = oneOfSchemas.get(1);
assertTrue(refDogSchema != null);
assertEquals(refDogSchema.get$ref(), "#/components/schemas/Dog");
Schema otherSchema = oneOfSchemas.get(2);
assertTrue(otherSchema != null);
Schema nameProp = (Schema) otherSchema.getProperties().get("name");
assertTrue(nameProp != null);
assertEquals(nameProp.getType(), "string");
}
use of io.swagger.v3.parser.OpenAPIV3Parser in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testIssue1072.
@Test
public void testIssue1072() throws Exception {
String yaml = "openapi: 3.0.0\n" + "info:\n" + " title: Test\n" + " version: 1.0.0\n" + "\n" + "paths:\n" + " /value:\n" + " get:\n" + " operationId: getValues\n" + " responses:\n" + " 200:\n" + " description: Successful response\n" + " content:\n" + " application/json:\n" + " schema:\n" + " $ref: '#/components/schemas/ComponentA'\n" + "components:\n" + " schemas:\n" + " ComponentA:\n" + " description: Component A\n" + " type: object\n" + " allOf:\n" + " - type: object\n" + " properties:\n" + " attributeWithoutType:\n" + " allOf:\n" + " - $ref: '#/components/schemas/ComponentB'\n" + " default: \"coucou\"\n" + " attributeWithWrongType:\n" + " type: object\n" + " allOf:\n" + " - $ref: '#/components/schemas/ComponentB'\n" + " default: \"coucou\"\n" + " correctAttribute:\n" + " type: string\n" + " allOf:\n" + " - $ref: '#/components/schemas/ComponentB'\n" + " default: \"coucou\"\n" + " ComponentB:\n" + " description: Component B\n" + " type: string";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
SwaggerParseResult result = parser.readContents(yaml, null, null);
OpenAPI openAPI = result.getOpenAPI();
assertNotNull(openAPI);
}
use of io.swagger.v3.parser.OpenAPIV3Parser in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testParamContent.
@Test
public void testParamContent() {
String json = "{" + " \"openapi\": \"3.0.0\"," + " \"info\": {" + " \"title\": \"Operations\"," + " \"version\": \"0.0.0\"" + " }," + " \"paths\": {" + " \"/operations\": {" + " \"post\": {" + " \"parameters\": [" + " {" + " \"name\": \"param0\"," + " \"in\": \"query\"," + " \"content\": {" + " }" + " }," + " {" + " \"name\": \"param1\"," + " \"in\": \"query\"," + " \"content\": {" + " \"text/plain\": {" + " }" + " }" + " }," + " {" + " \"name\": \"param2\"," + " \"in\": \"query\"," + " \"content\": {" + " \"text/plain\": {" + " }," + " \"application/json\": {" + " \"schema\": {" + " \"type\": \"object\"" + " }" + " }" + " }" + " }" + " ]," + " \"responses\": {" + " \"default\": {" + " \"description\": \"None\"" + " }" + " }" + " }" + " }" + " }" + "}";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
SwaggerParseResult result = parser.readContents(json, null, null);
Operation post = result.getOpenAPI().getPaths().get("/operations").getPost();
Parameter param0 = post.getParameters().stream().filter(p -> "param0".equals(p.getName())).findFirst().orElseThrow(() -> new IllegalStateException("Can't find parameter=param0"));
assertEquals(result.getMessages().contains("attribute paths.'/operations'(post).parameters.[param0].content with no media type is unsupported"), true, "No media types error reported");
assertEquals(param0.getContent(), null, "Empty content");
Parameter param1 = post.getParameters().stream().filter(p -> "param1".equals(p.getName())).findFirst().orElseThrow(() -> new IllegalStateException("Can't find parameter=param1"));
assertEquals(param1.getContent().size(), 1, "Valid content size");
Parameter param2 = post.getParameters().stream().filter(p -> "param2".equals(p.getName())).findFirst().orElseThrow(() -> new IllegalStateException("Can't find parameter=param2"));
assertEquals(result.getMessages().contains("attribute paths.'/operations'(post).parameters.[param2].content with multiple media types is unsupported"), true, "Multiple media types error reported");
assertEquals(param2.getContent(), null, "Content with multiple media types");
assertEquals(result.getMessages().size(), 2, "Messages");
}
use of io.swagger.v3.parser.OpenAPIV3Parser in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testDeserializeByteString.
@Test
public void testDeserializeByteString() {
String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + " version: 0.0.0\n" + " title: My 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" + " bytes:\n" + " $ref: '#/components/schemas/ByteString'\n" + "components:\n" + " schemas:\n" + " ByteString:\n" + " type: string\n" + " format: byte\n" + " default: W.T.F?\n" + " enum:\n" + " - VGhlIHdvcmxk\n" + " - aXMgYWxs\n" + " - dGhhdCBpcw==\n" + " - dGhlIGNhc2U=\n" + " - W.T.F?\n" + "";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
SwaggerParseResult result = parser.readContents(yaml, null, null);
final OpenAPI resolved = new OpenAPIResolver(result.getOpenAPI(), null).resolve();
Schema byteModel = resolved.getComponents().getSchemas().get("ByteString");
assertTrue(byteModel instanceof ByteArraySchema);
List<byte[]> byteValues = byteModel.getEnum();
assertEquals(byteValues.size(), 4);
assertEquals(new String(byteValues.get(0)), "The world");
assertEquals(new String(byteValues.get(1)), "is all");
assertEquals(new String(byteValues.get(2)), "that is");
assertEquals(new String(byteValues.get(3)), "the case");
assertEquals(byteModel.getDefault(), null);
assertEquals(result.getMessages(), Arrays.asList("attribute components.schemas.ByteString.enum=`W.T.F?` is not of type `byte`", "attribute components.schemas.ByteString.default=`W.T.F?` is not of type `byte`"));
}
use of io.swagger.v3.parser.OpenAPIV3Parser in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testDefinitions.
@Test
public void testDefinitions() {
String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + " version: ''\n" + " title: ''\n" + "paths: {}\n" + "components:\n" + " schemas:\n" + " invalid: true\n" + " Person:\n" + " required:\n" + " - id\n" + " - name\n" + " properties:\n" + " id:\n" + " type: integer\n" + " format: int64\n" + " name:\n" + " type: string";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
SwaggerParseResult result = parser.readContents(yaml, null, null);
List<String> messageList = result.getMessages();
Set<String> messages = new HashSet<>(messageList);
assertTrue(messages.contains("attribute components.schemas.invalid is not of type `object`"));
assertTrue(result.getOpenAPI().getComponents().getSchemas().get("Person") instanceof Schema);
List<String> required = ((Schema) result.getOpenAPI().getComponents().getSchemas().get("Person")).getRequired();
Set<String> requiredKeys = new HashSet<String>(required);
assertTrue(requiredKeys.contains("id"));
assertTrue(requiredKeys.contains("name"));
assertTrue(requiredKeys.size() == 2);
}
Aggregations