Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add a constructor that accepts AaiMessage to ChangeHeadersRefCommand_Aai20 to fix change of message headers ref in apicurio #803

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.apicurio.datamodels.Library;
import io.apicurio.datamodels.asyncapi.models.AaiHeaderItem;
import io.apicurio.datamodels.asyncapi.models.AaiMessage;
import io.apicurio.datamodels.asyncapi.models.AaiOperation;
import io.apicurio.datamodels.asyncapi.v2.models.Aai20NodeFactory;
import io.apicurio.datamodels.cmd.AbstractCommand;
Expand All @@ -28,12 +29,11 @@
/**
* A command used to change the $ref inside an AsyncAPI message headers.
* @author laurent.broudoux@gmail.com
* @deprecated Use {@link ChangePropertyCommand} instead.
*/
@Deprecated
public class ChangeHeadersRefCommand_Aai20 extends AbstractCommand {

public NodePath _operationPath;
public NodePath _messagePath;
public String _headersRef;
public String _oldHeadersRef = null;

Expand All @@ -47,22 +47,27 @@ public class ChangeHeadersRefCommand_Aai20 extends AbstractCommand {
this._headersRef = headersRef;
}

ChangeHeadersRefCommand_Aai20(String headersRef, AaiMessage messageNode) {
this._messagePath = Library.createNodePath(messageNode);
this._headersRef = headersRef;
}

@Override
public void execute(Document document) {
LoggerCompat.info("[ChangeHeadersRefCommand_Aai20] Executing.");

AaiOperation operation = (AaiOperation) this._operationPath.resolve(document);
this._changed = false;

if (this.isNullOrUndefined(operation) || this.isNullOrUndefined(operation.message) || !isValidRef(this._headersRef)) {

AaiMessage message = getMessage(document);
if (this.isNullOrUndefined(message) || !isValidRef(this._headersRef)) {
return;
}

AaiHeaderItem headerItem = operation.message.headers;
AaiHeaderItem headerItem = message.headers;
if (headerItem == null) {
Aai20NodeFactory nodeFactory = new Aai20NodeFactory();
headerItem = nodeFactory.createHeaderItem(operation.message);
operation.message.headers = headerItem;
headerItem = nodeFactory.createHeaderItem(message);
message.headers = headerItem;
}
if (headerItem.$ref != null) {
this._oldHeadersRef = headerItem.$ref;
Expand All @@ -75,20 +80,34 @@ public void execute(Document document) {
public void undo(Document document) {
LoggerCompat.info("[ChangeHeadersRefCommand_Aai20] Reverting.");

AaiOperation operation = (AaiOperation) this._operationPath.resolve(document);
AaiMessage message = getMessage(document);

if (!this._changed || this.isNullOrUndefined(operation)
|| this.isNullOrUndefined(operation.message)) {
if (!this._changed || this.isNullOrUndefined(message)) {
return;
}

AaiHeaderItem headerItem = operation.message.headers;
AaiHeaderItem headerItem = message.headers;
if (this._oldHeadersRef != null) {
headerItem.$ref = this._oldHeadersRef;
} else {
headerItem.$ref = null;
}
}

private AaiMessage getMessage(Document document) {
if (!this.isNullOrUndefined(this._operationPath)) {
AaiOperation operation = (AaiOperation) this._operationPath.resolve(document);

if (this.isNullOrUndefined(operation) || this.isNullOrUndefined(operation.message)) {
return null;
}

return operation.message;
} else {
return (AaiMessage) this._messagePath.resolve(document);
}

}

private boolean isValidRef(String refCandidate) {
return ModelUtils.isDefined(refCandidate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,13 +670,15 @@ public static final ICommand createChangePayloadRefCommand_Aai20(String payloadR
}

/**
* @deprecated Use createChangePropertyCommand instead
* @deprecated Use the AaiMessage variant
*/
@Deprecated
public static final ICommand createChangeHeadersRefCommand_Aai20(String headersRef, AaiOperation operation) {
return new ChangeHeadersRefCommand_Aai20(headersRef, operation);
}

public static final ICommand createChangeHeadersRefCommand_Aai20(String headersRef, AaiMessage message) {
return new ChangeHeadersRefCommand_Aai20(headersRef, message);
}

/* *** Delete Commands *** */

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"asyncapi": "2.0.0",
"info": {
"title": "Blank API",
"version": "1.0.0"
},
"channels": {
"myChannel": {
"description": "This is myChannel description",
"subscribe": {
"message":{
"payload": {}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"asyncapi": "2.0.0",
"info": {
"title": "Blank API",
"version": "1.0.0"
},
"channels": {
"myChannel": {
"description": "This is myChannel description",
"subscribe": {
"message":{
"payload": {}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[{
"__type": "ChangeHeadersRefCommand_Aai20",
"_headersRef": "NotAReference",
"_messagePath": "/channels[myChannel]/subscribe/message"
}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"asyncapi": "2.0.0",
"info": {
"title": "Blank API",
"version": "1.0.0"
},
"channels": {
"myChannel": {
"description": "This is myChannel description",
"subscribe": {
"message":{
"payload": {},
"headers": {
"$ref": "#/components/schema/UserSignedUpHeaders"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"asyncapi": "2.0.0",
"info": {
"title": "Blank API",
"version": "1.0.0"
},
"channels": {
"myChannel": {
"description": "This is myChannel description",
"subscribe": {
"message":{
"payload": {}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[{
"__type": "ChangeHeadersRefCommand_Aai20",
"_headersRef": "#/components/schema/UserSignedUpHeaders",
"_messagePath": "/channels[myChannel]/subscribe/message"
}]
6 changes: 4 additions & 2 deletions src/test/resources/fixtures/cmd/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,10 @@
{ "name": "[AsyncAPI 2] {Add Security Requirement} - Add Security Req", "test": "commands/add-security-requirement/asyncapi-2/add-security-requirement" },
{ "name": "[AsyncAPI 2] {Change Payload Ref} - Change Payload Ref via Operation", "test": "commands/change-payload-ref/asyncapi-2/change-payload-ref" },
{ "name": "[AsyncAPI 2] {Change Payload Ref} - Change Payload Ref via Message", "test": "commands/change-payload-ref/asyncapi-2/change-payload-ref-msg" },
{ "name": "[AsyncAPI 2] {Change Headers Ref} - Change Headers Ref", "test": "commands/change-headers-ref/asyncapi-2/change-headers-ref" },
{ "name": "[AsyncAPI 2] {Change Headers Ref} - Change Headers Ref Invalid", "test": "commands/change-headers-ref/asyncapi-2/change-headers-ref-invalid" },
{ "name": "[AsyncAPI 2] {Change Headers Ref} - Change Headers Ref via Operation", "test": "commands/change-headers-ref/asyncapi-2/change-headers-ref" },
{ "name": "[AsyncAPI 2] {Change Headers Ref} - Change Headers Ref Invalid via Operation", "test": "commands/change-headers-ref/asyncapi-2/change-headers-ref-invalid" },
{ "name": "[AsyncAPI 2] {Change Headers Ref} - Change Headers Ref via Message", "test": "commands/change-headers-ref/asyncapi-2/change-headers-ref-msg" },
{ "name": "[AsyncAPI 2] {Change Headers Ref} - Change Headers Ref Invalid via Message", "test": "commands/change-headers-ref/asyncapi-2/change-headers-ref-invalid-msg" },
{ "name": "[AsyncAPI 2] {Change Security Scheme} - Change Security Scheme", "test": "commands/change-security-scheme/asyncapi-2/change-security-scheme" },
{ "name": "[AsyncAPI 2] {Change Server} - Change Server", "test": "commands/change-server/asyncapi-2/change-server" },
{ "name": "[AsyncAPI 2] {Change Property Type} - Change Property Type", "test": "commands/change-property-type/asyncapi-2/change-property-type" },
Expand Down