Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions annot/src/main/java/com/predic8/membrane/annot/MCAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,20 @@
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Injects an XML attribute using a setter method.
* Annotation for marking a method to represent an attribute in the Membrane configuration.
* Primarily used for defining properties and their representation across different configuration grammars,
* such as JSON Schema or XML Schema (XSD).
*
* Methods annotated with this are typically setters with specific behavior or constraints
* controlled by the annotation's attributes.
*
* Attributes:
* - attributeName: Specifies the name of the attribute. If not defined, a default naming convention might apply.
* - excludeFromJson: Indicates whether the attribute should be excluded from JSON Schema representation (default is false).
*/
@Target(METHOD)
@Retention(RUNTIME)
public @interface MCAttribute {

String attributeName() default "";
boolean excludeFromJson() default false; // excludes from JSON Schema (YAML)
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ private void handle(Model m, MainInfo main, ElementInfo ei) throws XMLStreamExce
xew.writeAttribute("mixed", "true");
xew.writeAttribute("topLevel", Boolean.toString(ei.getAnnotation().topLevel()));
xew.writeAttribute("id", ei.getId());
xew.writeAttribute("deprecated", Boolean.toString(ei.isDeprecated()));
if (!ei.getAnnotation().topLevel()) {
String primaryParentId = getPrimaryParentId(m, main, ei);
if (primaryParentId != null)
Expand Down Expand Up @@ -198,6 +199,7 @@ private void handle(Model m, MainInfo main, ChildElementInfo cei) throws XMLStre
xew.writeStartElement("child");
xew.writeAttribute("min", cei.isRequired() ? "1" : "0");
xew.writeAttribute("max", cei.isList() ? "unbounded" : "1");
xew.writeAttribute("deprecated", Boolean.toString(cei.isDeprecated()));

handleDoc(cei);

Expand Down Expand Up @@ -228,6 +230,8 @@ private void handle(AttributeInfo ai) throws XMLStreamException {
xew.writeStartElement("attribute");
xew.writeAttribute("name", ai.getXMLName());
xew.writeAttribute("required", Boolean.toString(ai.isRequired()));
xew.writeAttribute("excludeFromJson", Boolean.toString(ai.excludedFromJsonSchema()));
xew.writeAttribute("deprecated", Boolean.toString(ai.isDeprecated()));
handleDoc(ai);
xew.writeEndElement();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ private FileObject createFile(MainInfo main) throws IOException {

private void processMCAttributes(ElementInfo i, SchemaObject so) {
i.getAis().forEach(ai -> {

// skip attributes marked with @MCExcludeFromSchema
if (ai.excludedFromJsonSchema())
return;

// hide id only on top-level elements
if ("id".equals(ai.getXMLName()) && i.getAnnotation().topLevel()) {
return;
Expand Down Expand Up @@ -253,7 +258,6 @@ boolean isFlowFromWebSocket(ChildElementInfo cei) {
}

private AbstractSchema<?> processList(ElementInfo i, AbstractSchema<?> so, ChildElementInfo cei, ArrayList<SchemaObject> sos) {

SchemaObject items = object("items");

if (shouldGenerateParserType(cei)) {
Expand Down Expand Up @@ -293,7 +297,6 @@ private void addChildsAsProperties(Model m, MainInfo main, ChildElementInfo cei,
.ref("#/$defs/" + ei.getXSDTypeName(m)))
.description(getDescriptionContent(ei))
.required(cei.isRequired());

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
package com.predic8.membrane.annot.model;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;

import com.predic8.membrane.annot.MCAttribute;
import com.predic8.membrane.annot.MCElement;
Expand All @@ -24,7 +26,8 @@
* Common behavior of Javadoc handling for {@link MCAttribute}, {@link MCElement}, etc.
*/
public abstract class AbstractJavadocedInfo {
private Element docedE;
public static final String JAVA_LANG_DEPRECATED = "java.lang.Deprecated";
private Element docedE;
private boolean docGenerated;
private Doc doc;

Expand All @@ -50,4 +53,11 @@ public Doc getDoc(ProcessingEnvironment processingEnv) {
return doc = new Doc(processingEnv, javadoc, getDocedE());
}

public boolean isDeprecated() {
for (AnnotationMirror am : docedE.getAnnotationMirrors())
if (((TypeElement) am.getAnnotationType().asElement()).getQualifiedName().toString().equals(JAVA_LANG_DEPRECATED))
return true;
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public String getSchemaType(Types typeUtils) {
/**
* It is not checked how many values the enum has. There are enums link validateRequests of OpenAPIValidator
* that have more than 2 values but they are also booleans at the configuration level
* @return
*/
private boolean isEnumBoolean(Types typeUtils) {
return getEnumValues(typeUtils).contains("TRUE") && getEnumValues(typeUtils).contains("FALSE");
Expand All @@ -98,7 +97,6 @@ public boolean isBeanReference(Types typeUtils) {

/**
* Sets as side effect instance variables e.g. xsdType, isEnum, isBeanReference.
* @param typeUtils
*/
private void analyze(Types typeUtils) {
if (xsdType != null) // already analyzed?
Expand Down Expand Up @@ -195,4 +193,13 @@ public boolean isRequired() {
public void setRequired(boolean required) {
this.required = required;
}

/**
* Determines whether the current field or method should be excluded from the JSON schema during schema generation.
* @return {@code true} if the field or method is excluded from the JSON schema; {@code false} otherwise.
*/
public boolean excludedFromJsonSchema() {
return annotation != null && annotation.excludeFromJson();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* 2. If there is no response in the exchange, the body and contentType of the request is copied into a new response.
* </p>
* <p>
* The options <i>statusCode</i> and <i>contentType</i> will overwrite the values from the messages.
* The options <i>status</i> and <i>contentType</i> will overwrite the values from the messages.
* </p>
* <p>
* This plugin is useful together with the template plugin. See examples/template.
Expand All @@ -51,7 +51,7 @@ public class ReturnInterceptor extends AbstractInterceptor {

private static final Logger log = LoggerFactory.getLogger(ReturnInterceptor.class.getName());

private int statusCode = 200;
private int status = 200;
private String contentType = null;

@Override
Expand All @@ -76,9 +76,9 @@ private Response getOrCreateResponse(Exchange exc) throws IOException {
response = createResponseFromRequest(exc);
}

if (statusCode != 0) {
response.setStatusCode(statusCode);
response.setStatusMessage(getMessageForStatusCode(statusCode));
if (status != 0) {
response.setStatusCode(status);
response.setStatusMessage(getMessageForStatusCode(status));
}

if (contentType!=null) {
Expand All @@ -92,7 +92,7 @@ private Response getOrCreateResponse(Exchange exc) throws IOException {
}

private Response createResponseFromRequest(Exchange exc) throws IOException {
Response.ResponseBuilder builder = new Response.ResponseBuilder().status(statusCode);
Response.ResponseBuilder builder = new Response.ResponseBuilder().status(status);
String reqContentType = exc.getRequest().getHeader().getContentType();
if (reqContentType != null) {
builder.contentType(reqContentType);
Expand All @@ -112,7 +112,7 @@ public String getDisplayName() {

@Override
public String getShortDescription() {
return (contentType != null) ? format("Sends a response with a status code of %d and a content type of %s.", statusCode, contentType) : format("Sends a response with a status code of %d.", statusCode);
return (contentType != null) ? format("Sends a response with a status code of %d and a content type of %s.", status, contentType) : format("Sends a response with a status code of %d.", status);
}

@Override
Expand All @@ -121,21 +121,38 @@ public EnumSet<Flow> getAppliedFlow() {
}

/**
* @description HTTP status code to be returned.
* @deprecated Use status instead.
* @description HTTP status code to be returned. statusCode is kept only for backward compatibility use status instead.
* @default 200
* @example 400
*/
@MCAttribute
@Deprecated
@MCAttribute(excludeFromJson = true)
Comment thread
rrayst marked this conversation as resolved.
public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
// Is excluded from the JSON schema. But will be included in the XSD schema.
this.status = statusCode;
}

public int getStatusCode() {
return statusCode;
return status;
}
Comment thread
predic8 marked this conversation as resolved.

/**
* @description HTTP status code to be returned.
* @default 200
* @example 400
*/
@MCAttribute
public void setStatus(int status) {
this.status = status;
}

public int getStatus() {
return status;
}

/**
* @description Content type of the response. If not set, the content type of the request (if available) or no content type will be used.
* @description Content-Type of the response. If not set, the content type of the request (if available) or no content type will be used.
* @default null
* @example application/json; charset=utf-8
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void routerConfConfig() {
contentType: application/json
src: '{ "ok": 1 }'
- return:
statusCode: 200
status: 200
---
api:
port: 2000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ api:
contentType: text/plain
src: Ordinary error!
- return:
statusCode: 500
status: 500
- if:
test: param.case == 'd'
flow:
Expand All @@ -115,6 +115,6 @@ api:
<description>XML Fehler Meldung vom Backend!</description>
</failure>
- return:
statusCode: 500
status: 500
# SOAP Service Mock for Debugging
- sampleSoapService: {}
2 changes: 1 addition & 1 deletion distribution/examples/extending-membrane/if/apis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ api:
- template:
src: Success
- return:
statusCode: 302
status: 302
6 changes: 3 additions & 3 deletions distribution/examples/monitoring-tracing/prometheus/apis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ api:
port: 2001
flow:
- return:
statusCode: 200
status: 200

---

api:
port: 2002
flow:
- return:
statusCode: 404
status: 404

---

api:
port: 2003
flow:
- return:
statusCode: 500
status: 500
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ api:
- template:
src: Success!
- return:
statusCode: 200
status: 200
2 changes: 1 addition & 1 deletion distribution/examples/openapi/validation-simple/apis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ api:
contentType: application/json
src: '{ "success": true }'
- return:
statusCode: 201
status: 201

# See examples/openapi-validator for a more detailed example
4 changes: 2 additions & 2 deletions distribution/examples/openapi/validation/apis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ api:
"email": "foo@baz.org"
}
- return:
statusCode: 201
status: 201

---

Expand All @@ -53,7 +53,7 @@ api:
}
] }
- return:
statusCode: 200
status: 200

---
# Monitoring endpoint for prometheus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ api:
- static:
src: Please log in!
- return:
statusCode: 401
status: 401
6 changes: 3 additions & 3 deletions distribution/examples/routing-traffic/shadowing/apis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ api:
flow:
- log: {}
- return:
statusCode: 200
status: 200

---

Expand All @@ -35,7 +35,7 @@ api:
flow:
- log: {}
- return:
statusCode: 201
status: 201

---

Expand All @@ -44,4 +44,4 @@ api:
flow:
- log: {}
- return:
statusCode: 202
status: 202
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ api:
"success": true
}
- return:
statusCode: 200
status: 200
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ api:
"success": true
}
- return:
statusCode: 200
status: 200
2 changes: 1 addition & 1 deletion distribution/examples/templating/json/apis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ api:
}
# To forward to backend use target below instead of return
- return:
statusCode: 200
status: 200
# target:
# host: YourBackendHost
# port: YourBackendPort
Expand Down
Loading
Loading