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

Duplicate variable names #346

Open
jmleyman opened this issue Nov 14, 2023 · 0 comments
Open

Duplicate variable names #346

jmleyman opened this issue Nov 14, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@jmleyman
Copy link

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 jmleyman added the bug Something isn't working label Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant