Reference swagger.yaml definitions from the Java API - java

I have a Spring Boot application that uses the Swagger API to auto-generate Swagger documentation. There is an old swagger.yaml file that contains a lot of object definitions. Instead of re-writing these in Java classes I would like to access the swagger.yaml file and use the existing definitions. Is this somehow possible?
Something along the lines of:
#Schema(name = "name", description = "description", ref = definition from swagger.yaml)
private SomeClass someClass;

Related

C++ Representing Objects in a YAML file

I used to work with the Java framework spring boot which has this neat functionality where
you can have an yaml file like this:
config:
example_int: 17
And a java class like this:
public class Config {
int example_int;
config(example_int) {
this.example_int = example_int;
}
}
The framework would then (if I remember correctly) inject into the runtime
an instance of class Config with example_int data member initialized to 17.
I'm looking to implement a similar functionality in C++
i.e. parse a yaml file and then construct a c++ object based on the files contents.
While (I think) Spring uses runtime injection I think I could do this via meta programming to
reduce complexity.
TLDR:
Parse yaml with C++
Based on yaml configs inject an object into the runtime, or generate code via
metaprogramming, which has the characteristics defined in the yaml file.

how to generate only openapi schemas from java/kotlin data classes? (for vert.x based application)

I am trying to create an open API spec for an existing vert.x based application, and since there is no library to generate the open API spec automatically for vert.x based apis, I am wondering if there is a plugin or a tool that can help me generate at least the corresponding schemas for my data classes since I have a lot of them and it is really painful to manually create the corresponding schema for each one of them?
I am using kotlin and Jackson for serialization/deserialization.
I know of no such library. Usually I would recommend to choose the reverse procedure which is to generate the model classes from your open api specification. For that, there is a really usefull library called openapi-generator.
The additional advantage of this approach would be, that you can use a fully api-first approach by also using the Vert.x extension called Web OpenAPI
You can use swagger java tools directly. Generation of swagger model from classes/annotations provides swagger-core library - see github or maven repo.
If you for example have annotated method:
public interface EndPoints {
#Operation(summary = "Delete product by ID ", method = "DELETE", operationId = "product/:deleteProductId",
tags = {
"Product"
},
parameters = {
#Parameter(in = ParameterIn.PATH, name = "deleteProductId",
required = true, description = "The ID for the Product", schema = #Schema(type = "string"))
},
responses = {
#ApiResponse(responseCode = "404", description = "Not found."),
#ApiResponse(responseCode = "200", description = "Product deleted."),
#ApiResponse(responseCode = "500", description = "Internal Server Error.")
}
)
void deleteProduct();
}
you can generate API doc using following code:
Reader openApiReader = new Reader();
Operation operation = openApiReader.parseMethod(EndPoints.class..getDeclaredMethod("deleteProduct"), Lists.newArrayList(), null);
And for your specific use-case, if you want to only generate Schema for given class (and use it for example as request body or return value), you can do for example:
public class ErrorApiResponse {
public String error;
public String severity;
public String message;
}
ResolvedSchema resolvedSchema = ModelConverters.getInstance()
.resolveAsResolvedSchema(new AnnotatedType(ErrorApiResponse.class));
You can construct io.swagger.v3.oas.models.OpenAPI manually, add generated Schemas to respective places. To construct JSON version of this API model, you simple call:
io.swagger.v3.core.util.Json.pretty(openAPI)

Swagger with Programmatic REST API(Jersey)

I tried searching for quite sometime and couldn't find the solution thus posting this question.
I have a Java Web project where we generate REST APIs programmatically(ie WITHOUT Jersey annotations like #Path/#Get etc) based on files(proprietary xml) provided by another tool which is kind of a standard in my organization through Jersey(ResourceConfig eg https://docs.huihoo.com/jersey/2.13/resource-builder.html) and we have a need of generating its corresponding SWAGGER docs.
All the tutorials that I found were using declarative API including page eg. https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5
I also looked into Swagger-Inflector which makes use of swagger.yaml file but in my case we need to generate it runtime with no manual work.
This is how(similar sample) we generate REST api:
final Resource.Builder resourceBuilder = Resource.builder();
resourceBuilder.path("helloworld");
final ResourceMethod.Builder methodBuilder = resourceBuilder.addMethod("GET");
methodBuilder.produces(MediaType.TEXT_PLAIN_TYPE)
.handledBy(new Inflector<ContainerRequestContext, String>() {
#Override
public String apply(ContainerRequestContext containerRequestContext) {
return "Hello World!";
}
});
final Resource resource = resourceBuilder.build();
registerResources(resource);
Is this feasible? Can we generate Swagger docs for programmatically generated REST APIs?

FlexJson and #JSON annotation - #JSON(name = "") not working

I just switched to FlexJson and I am already having a problem.
The documentation at http://flexjson.sourceforge.net/, chapter Controlling JSON naming with #JSON states :
When an object is serialized the names of the properties on JSON
objects are determined from the name of the Java property. This means
the names of the getter methods or the field names are used. However,
sometimes the JSON output doesn't match the names used by the Java
object. You can control this using the #JSON annotation. For example:
public class Species {
#JSON(jsonName = "genus")
private String type;
private String name;
#JSON(jsonName="species")
public String getName() {
return name;
}
}
Except it doesn't work. And then they say :
Defining a #JSON.jsonName is used in both serialization and
deserialization.
Now when I have a look into the javadocs at http://flexjson.sourceforge.net/javadoc/index.html, I can see there are 4 optional elements belonging to the #JSON annotation, those are
include name objectFactory transformer
None of it is jsonName, like in the example.
So how do I get this annotation to work, so I can have different java and json names?
How can I define this annotation element or make use of the predefined name
To clarify, I can annotate #JSON and autocomplete recommends #JSON(include), but then include cannot be resolved...
I am using FlexJson 2.1, and I imported flexjson.JSON;
Btw I am aware of this answer https://stackoverflow.com/a/8879616/2001247, but it's not what I want. I want to use annotations.
You need to use FlexJson 3.2
The problem is, latest jar accessible via developer site have 2.1 version number.
But java doc corresponds to version 3.2.
You can find FlexJson 3.2 jar in maven repository.

JAX-WS and Enunciate - How to change Enunciate's default XSD naming convention

I'm using Enunciate to generate a SOAP endpoint for a Wicket web application I am working on and I have a couple of questions that I haven't figured out the solution to yet.
1 How do I change the name of the xsd files? I've looked through the FAQ and it tells me to do something similar to this:
<xml>
<schema namespace="http://api.example.com/data" file="data.xsd"/>
</xml>
However, I haven't quite figured out how to set the targetNamespace for my data objects. I've done this for my service via #WebService ( targetNamespace="blah" ), but how do I annotate my data objects to let Enunciate know which namespace they should belong to?
2 Enunciate generates my XSDs just fine, but I don't particularily like the element names it uses. I have a ServiceRequest and ServiceResponse object. The ServiceRequest object has a List of User objects. The ServiceResponse has a list of Group objects. Enunciate suggests that every "User" object within the ServiceRequest should be using the tag "<users>". I feel that it would make more sense to use the singular form, "<user>" since the tag does in fact only contain a single user. Is it possible to change this behaviour, and if so, how?
Thanks in advance.
So just to be clear, with the exception of the question about naming your schema files, your questions are really more about JAXB than they are about Enunciate. JAXB is the spec that defines how your Java objects are (de)serialized to/from XML and Enunciate conforms to that spec.
Anyway, the easiest way to apply a namespace to your Java objects is with a package-info.java file in the package of your Java classes. Annotate your package with #XmlSchema and set the namespace to be the value you want.
Customizing how your accessors are serialized to/from XML can be done with the #XmlElement annotation, e.g.:
public class MyClass {
...
#XmlElement (name="user")
List<User> users;
...
}
Here are the JAXB javadocs
https://jaxb.dev.java.net/nonav/2.1.9/docs/api/
Or google for a good JAXB tutorial.

Categories