use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.
the class OpenAPIResolverTest method testResponseRemoteRefs.
private void testResponseRemoteRefs(String remoteRef) {
final OpenAPI swagger = new OpenAPI();
swagger.path("/fun", new PathItem().get(new Operation().responses(new ApiResponses().addApiResponse("200", new ApiResponse().content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().$ref(remoteRef))))))));
final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
final ApiResponse response = swagger.getPaths().get("/fun").getGet().getResponses().get("200");
final Schema ref = response.getContent().get("*/*").getSchema();
assertEquals(ref.get$ref(), "#/components/schemas/Tag");
assertNotNull(swagger.getComponents().getSchemas().get("Tag"));
}
use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.
the class OpenAPIResolverTest method testSimpleRemoteModelProperty.
private void testSimpleRemoteModelProperty(String remoteRef) {
final OpenAPI swagger = new OpenAPI();
swagger.components(new Components().addSchemas("Sample", new Schema().addProperties("remoteRef", new Schema().$ref(remoteRef))));
final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
final Schema prop = (Schema) resolved.getComponents().getSchemas().get("Sample").getProperties().get("remoteRef");
assertTrue(prop.get$ref() != null);
assertEquals(prop.get$ref(), "#/components/schemas/Tag");
assertNotNull(swagger.getComponents().getSchemas().get("Tag"));
}
use of io.swagger.v3.parser.OpenAPIResolver 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.OpenAPIResolver in project swagger-parser by swagger-api.
the class OpenAPIV3Parser method resolve.
private SwaggerParseResult resolve(SwaggerParseResult result, List<AuthorizationValue> auth, ParseOptions options, String location) {
try {
if (options != null) {
if (options.isResolve() || options.isResolveFully()) {
OpenAPIResolver resolver = new OpenAPIResolver(result.getOpenAPI(), emptyListIfNull(auth), location, null, options);
resolver.resolve(result);
if (options.isResolveFully()) {
new ResolverFully(options.isResolveCombinators()).resolveFully(result.getOpenAPI());
}
}
if (options.isFlatten()) {
final InlineModelResolver inlineModelResolver = new InlineModelResolver(options.isFlattenComposedSchemas(), options.isCamelCaseFlattenNaming(), options.isSkipMatches());
if (result.getOpenAPI() != null) {
inlineModelResolver.flatten(result.getOpenAPI());
}
}
}
} catch (Exception e) {
LOGGER.warn("Exception while resolving:", e);
// TODO verify if this change makes sense (adding resolve messages instead of replacing)
result.getMessages().add(e.getMessage());
// result.setMessages(Collections.singletonList(e.getMessage()));
}
return result;
}
use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.
the class OpenAPIResolverTest method testSettingsAddParametersToEachOperationDisabled.
@Test
public void testSettingsAddParametersToEachOperationDisabled() {
String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + " title: test spec\n" + " version: '1.0'\n" + "paths:\n" + " '/test/{id}':\n" + " parameters:\n" + " - name: id\n" + " in: path\n" + " required: true\n" + " schema:\n" + " type: string\n" + " get:\n" + " description: test get\n" + " parameters:\n" + " - name: page\n" + " in: query\n" + " schema:\n" + " type: string\n" + " responses:\n" + " default:\n" + " description: test response";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
SwaggerParseResult result = parser.readContents(yaml, null, null);
OpenAPI swagger = result.getOpenAPI();
final OpenAPI resolved = new OpenAPIResolver(swagger, null, null, new OpenAPIResolver.Settings().addParametersToEachOperation(false)).resolve();
assertEquals(resolved.getPaths().get("/test/{id}").getParameters().size(), 1);
PathParameter pp = (PathParameter) resolved.getPaths().get("/test/{id}").getParameters().get(0);
assertEquals(pp.getName(), "id");
assertEquals(resolved.getPaths().get("/test/{id}").getGet().getParameters().size(), 1);
QueryParameter qp = (QueryParameter) resolved.getPaths().get("/test/{id}").getGet().getParameters().get(0);
assertEquals(qp.getName(), "page");
}
Aggregations