public void loadSchemas() { for (final Resource resource : raml.getResources().values()) { loadSchemas(resource); } }
private String getApiName (Raml raml, String fileName) { String title = raml.getTitle(); return StringUtils.isNotBlank(title) ? title : fileName; }
@Override public void updateApi(String apiId, Raml raml, JSONObject config) { this.config = config; RestApi api = getApi(apiId); Optional<Resource> rootResource = getRootResource(api); createModels(api, raml.getSchemas(), true); createResources(api, createResourcePath(api, rootResource.get(), raml.getBasePath()), new HashMap<String, UriParameter>(), raml.getResources(), true); cleanupResources(api, this.paths); cleanupModels(api, this.models); }
@Override public String toString(Object o, String formatString, Locale locale) { final Raml raml = (Raml) o; if (raml.getBaseUri() == null) { return ""; } switch (formatString) { case "baseUri": if (raml.getProtocols() == null || raml.getProtocols().isEmpty()) { return raml.getBaseUri(); } final int pos = raml.getBaseUri().indexOf("://"); final String rest = pos < 0 ? raml.getBaseUri() : raml.getBaseUri().substring(pos + 3); if (raml.getProtocols().size() == 2) { return "http(s)://" + rest; } return raml.getProtocols().get(0).toString().toLowerCase() + "://" + rest; default: throw new IllegalArgumentException("unknown format '" + formatString + "'"); } } }
public String getBaseUri(Raml raml) { if (baseUri != null) { return baseUri; } String baseUri = raml.getBaseUri().replace("{version}", raml.getVersion()); if (baseUriParameters != null) { for (final String param : baseUriParameters.split(",")) { final String[] keyValue = param.split("="); if (keyValue.length != 2) { throw new IllegalArgumentException("baseUriParameters must be of the form 'key1=value1,key2=value2,...' but is '" + baseUriParameters + "'"); } baseUri = baseUri.replace("{" + keyValue[0] + "}", keyValue[1]); } } if (baseUri.contains("{")) { throw new IllegalArgumentException("Unresolved baseUri: '" + baseUri + "'. Use 'baseUri' or 'baseUriParameters' parameters to specify it."); } return baseUri; }
public RamlReport validate() { checker.parameters(raml.getBaseUriParameters(), BASE_URI); checker.description(raml.getDocumentation()); checker.description(raml.getBaseUriParameters(), BASE_URI); for (final Resource resource : raml.getResources().values()) { resource(resource); } return checker.getReport(); }
Raml raml = new Raml(); ObjectMapper m = new ObjectMapper(); raml.setVersion(definition.getVersion()); if (!definition.getEndpoints().isEmpty()) { endpoint = definition.getEndpoints().get(0); raml.setBaseUri(endpoint.computeUrl()); } else { raml.setBaseUri("http://example.com/v1"); raml.setSecuritySchemes(getSecuritySchemes(endpoint)); raml.setTitle(contract.getName()); raml.setResources(new LinkedHashMap<String, org.raml.model.Resource>()); fillResources(raml.getResources(), m, contract, representationSamples); raml.setSchemas(new ArrayList<Map<String, String>>()); Map<String, String> schemas = new LinkedHashMap<>(); raml.getSchemas().add(schemas); for (Representation representation : contract.getRepresentations()) { if (RamlUtils.isPrimitiveType(representation.getName())) {
if (raml.getVersion() != null) { definition.setVersion(raml.getVersion().substring(1)); contract.setName(raml.getTitle()); for (Entry<String, UriParameter> entry : raml.getBaseUriParameters() .entrySet()) { rootPathVariables.add(getPathVariable(entry.getKey(), for (Map<String, String> schema : raml.getSchemas()) { for (Entry<String, String> entry : schema.entrySet()) { Representation representation = new Representation(); for (Entry<String, org.raml.model.Resource> entry : raml.getResources() .entrySet()) { org.raml.model.Resource resource = entry.getValue();
@Test public void apiLevelDocs() { processResource("ApiLevelDocs.java", "raml", "all"); AssertJUnit.assertEquals("ApiLevelDocs should have produced exactly 1 results document", 1, output.size()); Map.Entry<String,String> entry = output.entrySet().iterator().next(); AssertJUnit.assertTrue("expected file named ApiLevelDocs-UltimateApi.raml", entry.getKey().endsWith("ApiLevelDocs-UltimateApi.raml")); Raml raml = new RamlDocumentBuilder().build(entry.getValue(), "http://example.com"); AssertJUnit.assertNotNull("RAML not parseable", raml); AssertJUnit.assertEquals("RAML title is incorrect", "The Ultimate REST API", raml.getTitle()); AssertJUnit.assertEquals("RAML version is incorrect", "v1", raml.getVersion()); AssertJUnit.assertEquals("RAML baseUri is incorrect", "/ultimate/api/v1", raml.getBaseUri()); List<DocumentationItem> documentation = raml.getDocumentation(); AssertJUnit.assertNotNull("RAML has no documentation items", documentation); AssertJUnit.assertEquals("RAML has too many documentation items", 1, documentation.size()); AssertJUnit.assertEquals("RAML documentation item has wrong title", "Overview", documentation.get(0).getTitle()); AssertJUnit.assertEquals("RAML documentation item has wrong content", "Some documentation of the API itself.", documentation.get(0).getContent().trim()); }
protected Raml buildRamlModel(final APIGroup apis) { final Raml raml = new Raml(); final API api = getFirstApi(apis); Preconditions.checkNotNull(api); raml.setBaseUri(api.getBaseUri()); raml.setVersion(api.getNamespace().getVersion()); // TODO raml.setSchemas(schemaResolver.resolve(api.getTypes())); // raml.setSecuredBy(); // raml.setSecuritySchemes(); raml.setDocumentation(documentationResolver.resolve(api)); // raml.setSchemas(); raml.setTitle(api.getName()); final ImmutableMap.Builder<String, org.raml.model.Resource> resources = ImmutableMap.builder(); for (final API singleApi : apis.getApis()) { resources.put(extractTrailingRoot(singleApi), createTopLevelResource(singleApi)); } raml.setResources(resources.build()); return raml; }
public JavaFile javaFile(Raml raml, String basePackage) { TypeSpec.Builder api = TypeSpec.classBuilder("Api" + capitalize(sanitize(raml.getTitle()))) .addJavadoc("$L api client\n", raml.getTitle()) .addJavadoc("Base URI pattern: $L\n", raml.getBaseUri()) .addField(configField.fieldSpec()) .addType(configClass.nestedConfSpec()) .addMethod(MethodSpec.constructorBuilder().addParameter(ClassName.bestGuess(NestedConfigClass.CONFIG_NESTED_STATIC_CLASS_NAME), configField.name()) .addStatement("this.$N = $N", configField.name(), configField.name()) .addModifiers(PRIVATE) .build()) .addModifiers(PUBLIC); api.addMethod(MethodSpec.methodBuilder(lowerCase(sanitize(raml.getTitle()))) .addParameter(ClassName.bestGuess(NestedConfigClass.CONFIG_NESTED_STATIC_CLASS_NAME), configField.name()) .returns(ClassName.bestGuess(api.build().name)) .addStatement("return new $N($N)", api.build().name, configField.name()) .addModifiers(PUBLIC, STATIC) .build()); raml.getResources().values().stream().forEach(resource -> api.addMethod( baseResource(resource, basePackage, configField.name(), configClass.getBaseSupplField().name()) )); return JavaFile.builder(basePackage, api.build()).build(); } }
public void generate() throws IOException { Path path = codegenConfig.getInputPath(); LOG.info("RAML: {}", path.toAbsolutePath()); if (path.endsWith(".raml")) { LOG.warn("Wrong path - should end with .raml"); return; } Raml raml = new RamlDocumentBuilder(new FileResourceLoader(path.getParent().toFile())) .build(path.getFileName().toString()); ReqSpecSupplField baseReqSpec = new ReqSpecSupplField(); ReqSpecField req = new ReqSpecField(); new RootApiClase(new NestedConfigClass(raml.getTitle(), baseReqSpec, req)) .javaFile(raml, codegenConfig.getBasePackage()) .writeTo(codegenConfig.getOutputPath()); raml.getResources().values().parallelStream().forEach(resource -> { new ResourceClassBuilder().withCodegenConfig(codegenConfig).withResource(resource).withReq(req).generate(); }); } }
protected API transformAPI(final Context context, final org.raml.model.Raml source, final org.raml.model.Resource sourceResourceRoot, final JSONSchemaStore store, final APINamespace namespace, final boolean async) { final APIBuilder apiBuilder = API.builder(); apiBuilder.namespace(namespace); apiBuilder.name(source.getTitle()); apiBuilder.qualifier(transformAPIName(sourceResourceRoot)); apiBuilder.path(transformAPIPath(source, sourceResourceRoot)); apiBuilder.baseUri(source.getBaseUri()); apiBuilder.async(async); final List<DocumentationItem> items = source.getDocumentation(); if (items != null && !items.isEmpty()) { apiBuilder.description(source.getDocumentation().get(0).getContent()); } final TransformationContext ctx = new TransformationContext(namespace, source, store); apiBuilder.parameterGroups(traitsTransformer.transform(context, source.getTraits(), ctx)); apiBuilder.types(dtosTransformer.transform(context, ctx)); if (!context.isCompatibilityModeDisabled()) { apiBuilder.types(enumerationsTransformer.transform(context, ctx)); } apiBuilder.resource(singleResourceTransformer.transform(context, sourceResourceRoot, ctx)); return apiBuilder.build(); }
@Override public APIGroup transform(final Context context, final org.raml.model.Raml source, final APIReference reference, final File globalSchemaStore) { final APINamespaceBuilder namespaceBuilder = APINamespace.builder(); namespaceBuilder.namespace(reference.getNamespace()); namespaceBuilder.packageName(reference.getPackageName()); namespaceBuilder.path(reference.getPath()); namespaceBuilder.version(source.getVersion()); namespaceBuilder.versionable(reference.isVersionable()); final APINamespace namespace = namespaceBuilder.build(); try (final JSONSchemaStore store = new JSONSchemaStore(source, globalSchemaStore, context)) { // Determine all of the root paths, and create one api per path final APIGroupBuilder groupBuilder = APIGroup.builder(); groupBuilder.namespace(namespace); for (final org.raml.model.Resource sourceResourceRoot : source.getResources().values()) { groupBuilder.api(transformAPI(context, source, sourceResourceRoot, store, namespace, reference.isAsync())); } return groupBuilder.build(); } catch (final IOException e) { throw new IllegalArgumentException(e); } }
public Map<String, String> getConsolidatedSchemas() { //HYBRIS start - changed HashMap to LinkedHashMap Map<String, String> consolidated = new LinkedHashMap<String, String>(); //HYBRIS end for (Map<String, String> map : getSchemas()) { consolidated.putAll(map); } return consolidated; }
private String getBasePath(final Raml source) { final String baseUri = source.getBaseUri(); if (baseUri == null || baseUri.isEmpty()) { return ""; } // skip protocol separator "//" int start = baseUri.indexOf("//") + 2; if (start == -1) { start = 0; } start = baseUri.indexOf('/', start); if (start == -1) { return ""; } return baseUri.substring(start); }
private void compileGlobalSchemas() { SchemaCompiler compiler = SchemaCompiler.getInstance(); Raml raml = getDocumentObject(); Map<String, Object> compiledSchemas = new HashMap<String, Object>(); List<Map<String, String>> schemas = raml.getSchemas(); for (Map<String, String> schemaMap : schemas) { compiledSchemas.putAll(compiler.compile(schemaMap)); } raml.setCompiledSchemas(compiledSchemas); }
@Test public void docTemplate() { String mountPoint = "/foo"; Utils.addTemplateValue(DocumentationRestApi.ID_TEMPLATE, "fooID"); Utils.addTemplateValue(DocumentationRestApi.MOUNT_TEMPLATE, mountPoint); processResource("ApiLevelTemplateDocs.java", "raml", "all"); Map.Entry<String, String> entry = output.entrySet().iterator().next(); entry.getKey(); AssertJUnit.assertTrue("expected file named AuthorizationScopes.raml", entry.getKey().endsWith("ApiLevelTemplateDocs-fooID.raml")); Raml raml = new RamlDocumentBuilder().build(entry.getValue(), "http://example.com"); AssertJUnit.assertNotNull("RAML not parseable", raml); List<DocumentationItem> documentation = raml.getDocumentation(); AssertJUnit.assertEquals("RAML baseUri is incorrect", mountPoint, raml.getBaseUri()); Resource resource = raml.getResource(mountPoint); AssertJUnit.assertNotNull("Cannot find resource related to:" + mountPoint, resource); }
protected String getPackage() { return context != null ? context.getPackageName() + DOT + ramlFile.getVersion() : dynamicNameSpace; }
private void checkBaseUriParameters(VariableMatcher hostMatch, VariableMatcher pathMatch, Action action) { final ParameterChecker paramChecker = new ParameterChecker(requestViolations).acceptUndefined(); final Map<String, List<? extends AbstractParam>> baseUriParams = getEffectiveBaseUriParams(config.raml.getBaseUriParameters(), action); paramChecker.checkListParameters(baseUriParams, hostMatch.getVariables(), new Message("baseUriParam", locator)); paramChecker.checkListParameters(baseUriParams, pathMatch.getVariables(), new Message("baseUriParam", locator)); }