Spring Rest Docs snippet template is ignored - java

I am attempting to be able to create custom snippet template for Spring Rest Docs documentation purposes. I have been following the reference guide
My first problem I ran into was in IntelliJ when creating a .snippet file in src/test/resources/org/springframework/restdocs/templates/asciidoctor/path-parameters.snippet as instructed by the guide. IntelliJ registers it as a jShell snippet file which from a quick glance is not the same as a AsciiDoc snippet. So I went into Settings -> Editor -> File Types and changed jShell Snippet from *.snippet to *.snippetOld. I then created a file type called Snippet and put it's pattern as *.snippet. This now fixes the problem that the snippet template is read as a jShell file. So the .snippet template I created no longer gets compile/validation errors.
But now when I run my mockMvc test after deleting the previous existing adoc files. The path-perameters.adoc file which should had used my custom template, if not use the default template, is now not generated at all.
In my mockMvc test I have the following
//Given
RestDocumentationResultHandler document = makeDocument("name-of-method");
document.document(
pathParameters(//do path parameters)
requestParameters(
parameterWithName("Month").description("The month requested").attributes(
key("type").value("integer"), key("constraints").value("more than 0 & less than 13.")
),
parameterWithName("Year").description("The year requested").attributes(
key("type").value("integer"), key("constraints").value("more than 1970 and less than current year")
)
),
responseField(//Do response fields)
);
// When
mvc.perform(get(REQUEST_PATH, USERID)
.contentType(MediaType.APPLICATION_JSON)
.param("month", "8")
.param("year", "2018"))
// Then
.andExpect(status().isOk())
.andDo(document);
And my snippet template is the following:
|===
|Parameter|Description|Type|Constraints
|{{parameter}}
|{{description}}
|{{type}}
|{{constraints}}
Could somebody point out what I've done wrong/different from the reference guide and how I could fix it to have my template working?

I suspect the reason you did not get output is that you confused path-parameters with request-parameters. Your documentation config specifies request parameters, but you customized path-parameters template
Here is the complete list of snippet templates:
curl-request.snippet
http-request.snippet
http-response.snippet
httpie-request.snippet
links.snippet
path-parameters.snippet
request-body.snippet
request-fields.snippet
request-headers.snippet
request-parameters.snippet
request-part-body.snippet
request-part-fields.snippet
request-parts.snippet
response-body.snippet
response-fields.snippet
response-headers.snippet
Source:
https://github.com/spring-projects/spring-restdocs/tree/master/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/asciidoctor

Related

Convert XPS-File to Text with Java

I wish to convert a .XPS File to Text with Java or Kotlin.
Aspose is to expensive for me.
I found Java-AXP, which should do the job but I did not find any documentation or sample code for it.
I managed to get the Java-AXP Core to my project libraries in IntelliJ. I can now access the files from within my project. But actually understanding how to use the library to get the text converstion is way beyond me.
The only attempt to use the library with example code I found here:
File b =new File("D:\\Chemia\\Clients\\Clients\\Docs\\Equipment\\CCP\\XPSOCRDemo.xps");
IXPSAccess access = new XPSFileAccessImpl(b);
IXPSFileAccess xpsFileAccess = access.getFileAccess();
XPSDocumentAccessImpl xpsimpl=new XPSDocumentAccessImpl(access);
int docunum = xpsimpl.getFirstDocNum();
IDocumentStructure structure=xpsimpl.getDocumentStructure(docunum);
List<IOutlineEntry> list=(List<IOutlineEntry>)
structure.getDocumentStructureOutline().getDocumentOutline().getOutlineEntry(
);
list.stream().forEach(restu->{
System.out.println( restu.getOutlineTarget());
});
However, I do get the same null pointer exeption as the OP and I can't fix it. I'd need a working example code to continue on my own.
So how can I use Java-AXP? Or are there alternative libraries? I am open to use anything.
Thanks for any help.
Edit:
#Abra Thanks.
Exception in thread "main" java.lang.IllegalStateException: structure must not be null
at MainKt.main(main.kt:80)
at MainKt.main(main.kt)
73 | val structure = xpsimpl.getDocumentStructure(0)
80 | val list = structure.documentStructureOutline?.documentOutline?.outlineEntry as List<IOutlineEntry>?
xpsimpl.getDocumentStructure(docunum) is null
I can not use a website for my purpose because the XPS files contain sensitive information.
Edit: Here is the solution I found.
I found this repo which uses Java-AXP. However there seemed to be some differences to the java-axp-core.jar I used from
Google Code
So I copied the javaaxp folder to my project.
It needed Apache Tika so I downloaded tika-app-2.1.0.jar from Apache Tika and included it in my project via the Project Structure menu.
In XPSZipFileAccess.java I had a wrong import for IOUtils so I changed
import org.apache.tika.io.IOUtils;
to
import org.apache.commons.io.IOUtils;
Now the Java-AXP could resolve all references.
Then I copied XPSParser.java to the root of my project and made sure all imports work.
In DocSaver.java on line 90 I found where XPSParser was used and I adapted the code so I could convert the XPS file to text:
val xpsFile = File(path + "filename.xps")
val inputStream = FileInputStream(xpsFile)
val metadata = Metadata()
val handler = BodyContentHandler()
XPSParser().parse(inputStream, handler, metadata, ParseContext())
val docContents = handler.toString()
println(docContents)
inputStream.close()
Hope it helps someone.
Here is the solution I found.
I found this repo which uses Java-AXP. However there seemed to be some differences to the java-axp-core.jar I used from
Google Code
So I copied the javaaxp folder to my project.
It needed Apache Tika so I downloaded tika-app-2.1.0.jar from Apache Tika and included it in my project via the Project Structure menu.
In XPSZipFileAccess.java I had a wrong import for IOUtils so I changed
import org.apache.tika.io.IOUtils;
to
import org.apache.commons.io.IOUtils;
Now the Java-AXP could resolve all references.
Then I copied XPSParser.java to the root of my project and made sure all imports work.
In DocSaver.java on line 90 I found where XPSParser was used and I adapted the code so I could convert the XPS file to text:
val xpsFile = File(path + "filename.xps")
val inputStream = FileInputStream(xpsFile)
val metadata = Metadata()
val handler = BodyContentHandler()
XPSParser().parse(inputStream, handler, metadata, ParseContext())
val docContents = handler.toString()
println(docContents)
inputStream.close()

Disable name prefix in javadoc

When generating the Javadoc, it adds on prefix to the imported class name, as shown below on the first line 'java.lang'.
How to properly disable that?
Have tried adding -noqualifier in Other command line arguments in my IntelliJ popup window but the following error occurred:
javadoc: error - Illegal package name: "/Users"
Below is a snippet from the Javadoc I generated:
public TrainRoute(java.lang.String name,
int routeNumber)
Creates a new TrainRoute with the given name and number.
Should meet the specification of Route.Route(String, int)
Parameters:
name - The name of the route.
routeNumber - The route number of the route.
I know this is an old question, but still relevant. I am aware of two solution which can be used alone or in combination:
To suppress prefixes on java packages use:
-noqualifier java.*
To suppress prefixes and link to the actual Java docs use:
-link https://docs.oracle.com/javase/8/docs/api
Both suppress the java name qualifiers. The second also links to the Oracle docs.
See javadoc options docs for more info.

Swagger Error Expected type string but found type integer

I started working recently on swagger to generate documentation for our RESTful apis. I use the yamlbeans-1.09.jar to convert my Java DTOs to a yaml file.
Everything works fine and the yaml is even validated properly on the http://www.yamllint.com/ but still while testing it with the Swagger interface, it gives an error "Expected type string but found type integer" and "No enum match for: 2" wherever I mention the swagger : 2.0 in the file.
My sample yml file is:
basePath: /employment
host: api.xxxx.com
schemes:
- https
swagger: 2.0
info:
title: API Doc
description: Description for the info goes here
version: 1.0.0
It always tends to give an error just a line number before the swagger version. Any help will be appreciated.
Edit: I have defined the swagger version as string in the java code and written as described below:
HashMap<String, Object> rootElementsForYml = new HashMap<String, Object>();
rootElementsForYml.put(SWAGGER, "2.0");
rootElementsForYml.put(HOST, "api.xxxx.com");
rootElementsForYml.put(BASEPATH, basePath);
rootElementsForYml.put(SCHEMES, new String[]{"https"});
But still it gives no result and the same error.
That's because the version value is a string and not a number. Just specifying 2.0 is interpreted as a number. Wrap it with quotes like "2.0" and it would work fine.

Route to upload file in Play Framework 2.10

I upload files to /upload folder, then I want to directly access my files, like:
http://localhost/upload/xxx.jpg
when I add routes as below:
GET /upload/*file controllers.Assets.at(path="/upload", file)
It causes another error:
not enough arguments for method at: (path: String, file: String)play.api.mvc.Call. Unspecified value parameter file.
<link rel="stylesheet" media="screen" href="#routes.Assets.at("stylesheets/main.css")">
Then, after I change #routes.Assets.at("stylesheets/main.css") to #routes.Assets.at("stylesheets/", "main.css"), there is another error:
[MatchError: (stylesheets/,main.css) (of class scala.Tuple2)]
(path: #unchecked, file: #unchecked) match {
Can somebody help me with this route? Thanks.
finnal, I got answer from playframework website, it not very obvious to find..
http://www.playframework.com/documentation/2.0.4/Assets
from this page:
However, if you define two mappings for the Assets.at action, like this:
GET /javascripts/*file controllers.Assets.at(path="/public/javascripts", file)
GET /images/*file controllers.Assets.at(path="/public/images", file)
Then you will need to specify both parameters when using the reverse router:
<script src="#routes.Assets.at("/public/javascripts", "jquery.js")"></script>
<image src="#routes.Assets.at("/public/images", "logo.png")">
but this may not solve my problem yet, it turn out to appear the second error mention in the question.
Be Careful, check the path param, it must be the same as you described in routes file. as:
when I set:
GET /public/*file controllers.Assets.at(path="/public", file)
in the html file, I should write as below:
#routes.Assets.at("/public", "stylesheets/main.css")
besides, if you use another folders, like /upload, adding below code in project/Build.scala in play.Project is essential. thanks TizianoPiccardi
playAssetsDirectories <+= baseDirectory / "foo"
You should add this line in project/Build.scala:
val main = play.Project(appName, appVersion, appDependencies).settings(
// Add your own project settings here
playAssetsDirectories <+= baseDirectory / "upload"
)
More info:
https://github.com/playframework/Play20/wiki/Assets

Creating index and adding mapping in Elasticsearch with java api gives missing analyzer errors

Code is in Scala. It is extremely similar to Java code.
Code that our map indexer uses to create index: https://gist.github.com/a16e5946b67c6d12b2b8
Utilities that the above code uses to create index and mapping: https://gist.github.com/4f88033204cd761abec0
Errors that java gives: https://gist.github.com/d6c835233e2b606a7074
Response of http://elasticsearch.domain/maps/_settings after running code and getting errors: https://gist.github.com/06ca7112ce1b01de3944
JSON FILES:
https://gist.github.com/bbab15d699137f04ad87
https://gist.github.com/73222e300be9fffd6380
Attached are the json files i'm loading in. I have confirmed that it is loading the right json files and properly outputting it as a string into .loadFromSource and .setSource.
Any ideas why it can't find the analyzers even though they are in _settings? If I run these json files via curl they work fine and properly setup the mapping.
The code I was using to create the index (found here: Define custom ElasticSearch Analyzer using Java API) was creating settings in the index like:
"index.settings.analysis.filter.my_snow.type: "stemmer","
It had settings in the setting path.
I changed my indexing code to the following to fix this:
def createIndex(client: Client, indexName: String, indexFile: String) {
//Create index
client.admin().indices().prepareCreate(indexName)
.setSource(Utils.loadFileAsString(indexFile))
.execute()
.actionGet()
}

Categories

Resources