Skip to content

Commit

Permalink
Allow users to pass in their own version of IfNonMatch header (#16813)
Browse files Browse the repository at this point in the history
  • Loading branch information
azabbasi authored Oct 26, 2020
1 parent b3189ba commit 3b45ee1
Show file tree
Hide file tree
Showing 41 changed files with 4,062 additions and 6,828 deletions.
10 changes: 10 additions & 0 deletions sdk/digitaltwins/azure-digitaltwins-core/autorest.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,13 @@ java:
custom-types-subpackage: models
required-fields-as-ctor-args: true
```
## This directive removes the specified enum values from the swagger so the code generator will expose IfNonMatch header as an option instead of always attaching it to requests with its only default value.
``` yaml

directive:
- from: swagger-document
where: $..[?(@.name=='If-None-Match')]
transform: delete $.enum;

```
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@ public Mono<DigitalTwinsAddResponse> addWithResponseAsync(
if (digitalTwinsAddOptions != null) {
digitalTwinsAddOptions.validate();
}
final String ifNoneMatch = "*";
String traceparentInternal = null;
if (digitalTwinsAddOptions != null) {
traceparentInternal = digitalTwinsAddOptions.getTraceparent();
Expand All @@ -363,6 +362,11 @@ public Mono<DigitalTwinsAddResponse> addWithResponseAsync(
tracestateInternal = digitalTwinsAddOptions.getTracestate();
}
String tracestate = tracestateInternal;
String ifNoneMatchInternal = null;
if (digitalTwinsAddOptions != null) {
ifNoneMatchInternal = digitalTwinsAddOptions.getIfNoneMatch();
}
String ifNoneMatch = ifNoneMatchInternal;
return service.add(
this.client.getHost(),
traceparent,
Expand Down Expand Up @@ -574,7 +578,6 @@ public Mono<DigitalTwinsAddRelationshipResponse> addRelationshipWithResponseAsyn
if (digitalTwinsAddRelationshipOptions != null) {
digitalTwinsAddRelationshipOptions.validate();
}
final String ifNoneMatch = "*";
String traceparentInternal = null;
if (digitalTwinsAddRelationshipOptions != null) {
traceparentInternal = digitalTwinsAddRelationshipOptions.getTraceparent();
Expand All @@ -585,6 +588,11 @@ public Mono<DigitalTwinsAddRelationshipResponse> addRelationshipWithResponseAsyn
tracestateInternal = digitalTwinsAddRelationshipOptions.getTracestate();
}
String tracestate = tracestateInternal;
String ifNoneMatchInternal = null;
if (digitalTwinsAddRelationshipOptions != null) {
ifNoneMatchInternal = digitalTwinsAddRelationshipOptions.getIfNoneMatch();
}
String ifNoneMatch = ifNoneMatchInternal;
return service.addRelationship(
this.client.getHost(),
traceparent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@ public static DigitalTwinsAddOptions toProtocolLayerOptions(CreateDigitalTwinOpt
return null;
}

return new DigitalTwinsAddOptions().setTraceparent(options.getTraceParent()).setTracestate(options.getTraceState());
return new DigitalTwinsAddOptions()
.setTraceparent(options.getTraceParent())
.setTracestate(options.getTraceState())
.setIfNoneMatch(options.getIfNoneMatch());
}

public static DigitalTwinsAddRelationshipOptions toProtocolLayerOptions(CreateRelationshipOptions options) {
if (options == null) {
return null;
}

return new DigitalTwinsAddRelationshipOptions().setTraceparent(options.getTraceParent()).setTracestate(options.getTraceState());
return new DigitalTwinsAddRelationshipOptions()
.setTraceparent(options.getTraceParent())
.setTracestate(options.getTraceState())
.setIfNoneMatch(options.getIfNoneMatch());
}

public static DigitalTwinModelsAddOptions toProtocolLayerOptions(CreateModelsOptions options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public final class DigitalTwinsAddOptions {
@JsonProperty(value = "tracestate")
private String tracestate;

/*
* Only perform the operation if the entity does not already exist.
*/
@JsonProperty(value = "If-None-Match")
private String ifNoneMatch;

/**
* Get the traceparent property: Identifies the request in a distributed tracing system.
*
Expand Down Expand Up @@ -65,6 +71,26 @@ public DigitalTwinsAddOptions setTracestate(String tracestate) {
return this;
}

/**
* Get the ifNoneMatch property: Only perform the operation if the entity does not already exist.
*
* @return the ifNoneMatch value.
*/
public String getIfNoneMatch() {
return this.ifNoneMatch;
}

/**
* Set the ifNoneMatch property: Only perform the operation if the entity does not already exist.
*
* @param ifNoneMatch the ifNoneMatch value to set.
* @return the DigitalTwinsAddOptions object itself.
*/
public DigitalTwinsAddOptions setIfNoneMatch(String ifNoneMatch) {
this.ifNoneMatch = ifNoneMatch;
return this;
}

/**
* Validates the instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public final class DigitalTwinsAddRelationshipOptions {
@JsonProperty(value = "tracestate")
private String tracestate;

/*
* Only perform the operation if the entity does not already exist.
*/
@JsonProperty(value = "If-None-Match")
private String ifNoneMatch;

/**
* Get the traceparent property: Identifies the request in a distributed tracing system.
*
Expand Down Expand Up @@ -65,6 +71,26 @@ public DigitalTwinsAddRelationshipOptions setTracestate(String tracestate) {
return this;
}

/**
* Get the ifNoneMatch property: Only perform the operation if the entity does not already exist.
*
* @return the ifNoneMatch value.
*/
public String getIfNoneMatch() {
return this.ifNoneMatch;
}

/**
* Set the ifNoneMatch property: Only perform the operation if the entity does not already exist.
*
* @param ifNoneMatch the ifNoneMatch value to set.
* @return the DigitalTwinsAddRelationshipOptions object itself.
*/
public DigitalTwinsAddRelationshipOptions setIfNoneMatch(String ifNoneMatch) {
this.ifNoneMatch = ifNoneMatch;
return this;
}

/**
* Validates the instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public final class CreateDigitalTwinOptions {
@JsonProperty(value = "tracestate")
private String tracestate;

/*
* If-Non-Match header that makes the request method conditional on a recipient cache or origin server either not having any current representation of the target resource.
* Acceptable values are null or "*".
* If IfNonMatch option is null the service will replace the existing entity with the new entity.
* If IfNonMatch option is "*" the service will reject the request if the entity already exists.
*/
@JsonProperty(value = "If-None-Match")
private String ifNoneMatch;

/**
* Get the traceparent property: Identifies the request in a distributed tracing system.
*
Expand All @@ -37,6 +46,20 @@ public String getTraceParent() {
return this.traceparent;
}

/**
* Get the ifNoneMatch property
*
* If-Non-Match header makes the request method conditional on a recipient cache or origin server either not having any current representation of the target resource.
* Acceptable values are null or "*".
* If IfNonMatch option is null the service will replace the existing entity with the new entity.
* If IfNonMatch option is "*" the service will reject the request if the entity already exists.
*
* @return the ifNoneMatch value.
*/
public String getIfNoneMatch() {
return this.ifNoneMatch;
}

/**
* Set the traceparent property: Identifies the request in a distributed tracing system.
*
Expand Down Expand Up @@ -69,4 +92,20 @@ public CreateDigitalTwinOptions setTraceState(String tracestate) {
this.tracestate = tracestate;
return this;
}

/**
* Set the ifNoneMatch property.
*
* If-Non-Match header makes the request method conditional on a recipient cache or origin server either not having any current representation of the target resource.
* Acceptable values are null or "*".
* If IfNonMatch option is null the service will replace the existing entity with the new entity.
* If IfNonMatch option is "*" the service will reject the request if the entity already exists.
*
* @param ifNoneMatch the ifNoneMatch value to set.
* @return the CreateRelationshipOptions object itself.
*/
public CreateDigitalTwinOptions setIfNoneMatch(String ifNoneMatch) {
this.ifNoneMatch = ifNoneMatch;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public final class CreateRelationshipOptions {
@JsonProperty(value = "tracestate")
private String tracestate;

/*
* If-Non-Match header that makes the request method conditional on a recipient cache or origin server either not having any current representation of the target resource.
* Acceptable values are null or "*".
* If IfNonMatch option is null the service will replace the existing entity with the new entity.
* If IfNonMatch option is "*" the service will reject the request if the entity already exists.
*/
@JsonProperty(value = "If-None-Match")
private String ifNoneMatch;

/**
* Get the traceparent property: Identifies the request in a distributed tracing system.
*
Expand Down Expand Up @@ -58,6 +67,20 @@ public String getTraceState() {
return this.tracestate;
}

/**
* Get the ifNoneMatch property
*
* If-Non-Match header makes the request method conditional on a recipient cache or origin server either not having any current representation of the target resource.
* Acceptable values are null or "*".
* If IfNonMatch option is null the service will replace the existing entity with the new entity.
* If IfNonMatch option is "*" the service will reject the request if the entity already exists.
*
* @return the ifNoneMatch value.
*/
public String getIfNoneMatch() {
return this.ifNoneMatch;
}

/**
* Set the tracestate property: Provides vendor-specific trace identification information and is a companion to
* traceparent.
Expand All @@ -69,4 +92,20 @@ public CreateRelationshipOptions setTraceState(String tracestate) {
this.tracestate = tracestate;
return this;
}

/**
* Set the ifNoneMatch property.
*
* If-Non-Match header makes the request method conditional on a recipient cache or origin server either not having any current representation of the target resource.
* Acceptable values are null or "*".
* If IfNonMatch option is null the service will replace the existing entity with the new entity.
* If IfNonMatch option is "*" the service will reject the request if the entity already exists.
*
* @param ifNoneMatch the ifNoneMatch value to set.
* @return the CreateRelationshipOptions object itself.
*/
public CreateRelationshipOptions setIfNoneMatch(String ifNoneMatch) {
this.ifNoneMatch = ifNoneMatch;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.azure.core.util.logging.ClientLogger;
import com.azure.digitaltwins.core.models.BasicDigitalTwin;
import com.azure.digitaltwins.core.models.BasicRelationship;
import com.azure.digitaltwins.core.models.CreateRelationshipOptions;
import com.azure.digitaltwins.core.models.IncomingRelationship;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -113,7 +114,12 @@ public void relationshipLifecycleTest(HttpClient httpClient, DigitalTwinsService
.verifyComplete();

// Create a relation which already exists - should return status code 409 (Conflict).
StepVerifier.create(asyncClient.createRelationship(roomTwinId, ROOM_CONTAINED_IN_FLOOR_RELATIONSHIP_ID, floorTwinContainedInRelationshipPayload, String.class))
StepVerifier.create(asyncClient.createRelationshipWithResponse(
roomTwinId,
ROOM_CONTAINED_IN_FLOOR_RELATIONSHIP_ID,
floorTwinContainedInRelationshipPayload,
String.class,
new CreateRelationshipOptions().setIfNoneMatch("*")))
.verifyErrorSatisfies(ex -> assertRestException(ex, HTTP_PRECON_FAILED));

// Update relationships
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ public void relationshipLifecycleTest(HttpClient httpClient, DigitalTwinsService

// Create a relation which already exists - should return status code 409 (Conflict).
assertRestException(
() -> client.createRelationship(roomTwinId, ROOM_CONTAINED_IN_FLOOR_RELATIONSHIP_ID, floorTwinContainedInRelationshipPayload, String.class),
() -> client.createRelationshipWithResponse(
roomTwinId,
ROOM_CONTAINED_IN_FLOOR_RELATIONSHIP_ID,
floorTwinContainedInRelationshipPayload,
String.class,
new CreateRelationshipOptions().setIfNoneMatch("*"),
Context.NONE),
HTTP_PRECON_FAILED
);

Expand Down
Loading

0 comments on commit 3b45ee1

Please sign in to comment.