java-spring-cloud-stream-template icon indicating copy to clipboard operation
java-spring-cloud-stream-template copied to clipboard

Duplicate variable names

Open jmleyman opened this issue 1 year ago • 0 comments

When you define a simple component's schemas in your AsynAPI definition file with 2 properties and 2 different names whicth refer to the same object, the generator will generate a class with 2 members with the same names (the same name as the type). The java class doens't compile and the addition @JsonProperty is not usefull (issue linked : @JsonProperty will not get imported in java file

  • The simple exemple to reproduce :
asyncapi: 2.6.0
info:
  title: Test
  version: '1.0'
  description: Test
channels:
  myChannel.v1:
    publish:
      operationId: sendMessage
      x-scs-function-name: sendMessage
      x-scs-group: sendMessage
      message:
        $ref: '#/components/messages/myChannelRequest'
components:
  messages:
    myChannelRequest:
      payload:
        $ref: '#/components/schemas/myChannelInfo'
  schemas:
    myChannelInfo:
      type: object
      properties:
        myPrincipalRef:
          $ref: '#/components/schemas/myReference'
        mySecondRef:
          $ref: '#/components/schemas/myReference'
    myReference:
      type: object
      properties:
        myString:
          type: string
  • The generated class :
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class MyChannelInfo {
	public MyChannelInfo () {
	}
	public MyChannelInfo (
		MyReference myReference, 
		MyReference myReference) {
		this.myReference = myReference;
		this.myReference = myReference;
	}
	@JsonProperty("myPrincipalRef")
	private MyReference myReference;
	@JsonProperty("mySecondRef")
	private MyReference myReference;
	public MyReference getMyReference() {
		return myReference;
	}
	public MyChannelInfo setMyReference(MyReference myReference) {
		this.myReference = myReference;
		return this;
	}
	public MyReference getMyReference() {
		return myReference;
	}
	public MyChannelInfo setMyReference(MyReference myReference) {
		this.myReference = myReference;
		return this;
	}
	public String toString() {
		return "MyChannelInfo ["
		+ " myReference: " + myReference
		+ " myReference: " + myReference
		+ " ]";
	}
}

The right class generation must be the same has generate by the Java Spring Template project (https://github.com/asyncapi/java-spring-template/tree/master) whitch generate this class :

import javax.validation.constraints.*;
import javax.validation.Valid;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
import java.util.List;
import java.util.Objects;
public class MyChannelInfo {
    private @Valid MyReference myPrincipalRef;
    private @Valid MyReference mySecondRef;
    @JsonProperty("myPrincipalRef")
    public MyReference getMyPrincipalRef() {
        return myPrincipalRef;
    }
    public void setMyPrincipalRef(MyReference myPrincipalRef) {
        this.myPrincipalRef = myPrincipalRef;
    }
    @JsonProperty("mySecondRef")
    public MyReference getMySecondRef() {
        return mySecondRef;
    }
    public void setMySecondRef(MyReference mySecondRef) {
        this.mySecondRef = mySecondRef;
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        MyChannelInfo myChannelInfo = (MyChannelInfo) o;
        return 
            Objects.equals(this.myPrincipalRef, myChannelInfo.myPrincipalRef) &&
            Objects.equals(this.mySecondRef, myChannelInfo.mySecondRef);
    }
    @Override
    public int hashCode() {
        return Objects.hash(myPrincipalRef, mySecondRef);
    }
    @Override
    public String toString() {
        return "class MyChannelInfo {\n" +
        
                "    myPrincipalRef: " + toIndentedString(myPrincipalRef) + "\n" +
                "    mySecondRef: " + toIndentedString(mySecondRef) + "\n" +
                "}";
    }
    /**
     * Convert the given object to string with each line indented by 4 spaces (except the first line).
     */
    private String toIndentedString(Object o) {
        if (o == null) {
           return "null";
        }
        return o.toString().replace("\n", "\n    ");
    }
}

jmleyman avatar Nov 14 '23 17:11 jmleyman