Skip to content

Commit

Permalink
Porting fix from autorest.java (#4371)
Browse files Browse the repository at this point in the history
Azure/autorest.java#2943

Co-authored-by: Weidong Xu <weidxu@microsoft.com>
  • Loading branch information
alzimmermsft and weidongxu-microsoft authored Sep 10, 2024
1 parent a714eea commit c6809aa
Show file tree
Hide file tree
Showing 19 changed files with 1,141 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,24 @@ public ClientModelPropertiesManager(ClientModel model, JavaSettings settings) {
}
}

// Temporary fix to a larger problem where the discriminator property is defined by a parent model, but not as
// a discriminator. This results in the discriminator property being serialized and deserialized twice as it
// shows up once as a regular property and once as a discriminator property. This will remove the regular
// property from the super properties and indicate that the discriminator came from a parent model.
if (discriminatorProperty != null) {
String serializedDiscriminatorName = discriminatorProperty.getProperty().getSerializedName();
ClientModelProperty removed;
if ((removed = superRequiredProperties.remove(serializedDiscriminatorName)) != null) {
discriminatorProperty = new ClientModelPropertyWithMetadata(model, removed.newBuilder()
.defaultValue(discriminatorProperty.getProperty().getDefaultValue()).build(),
true);
} else if ((removed = superSetterProperties.remove(serializedDiscriminatorName)) != null) {
discriminatorProperty = new ClientModelPropertyWithMetadata(model, removed.newBuilder()
.defaultValue(discriminatorProperty.getProperty().getDefaultValue()).build(),
true);
}
}

this.hasRequiredProperties = hasRequiredProperties;
this.requiredPropertiesCount = requiredProperties.size() + superRequiredProperties.size();
this.setterPropertiesCount = setterProperties.size() + superSetterProperties.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ $generateScript = {
}
}

Set-Location ../../
Set-Location (Resolve-Path (Join-Path $PSScriptRoot '..' '..'))

npm install
npm run build
npm pack

Set-Location ./generator/http-client-generator-test
Set-Location $PSScriptRoot


if (Test-Path node_modules) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) TypeSpec Code Generator.

package com.cadl.discriminatoredgecases;

import com.azure.core.annotation.Generated;
import com.azure.core.annotation.ReturnType;
import com.azure.core.annotation.ServiceClient;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.exception.ClientAuthenticationException;
import com.azure.core.exception.HttpResponseException;
import com.azure.core.exception.ResourceModifiedException;
import com.azure.core.exception.ResourceNotFoundException;
import com.azure.core.http.rest.RequestOptions;
import com.azure.core.http.rest.Response;
import com.azure.core.util.BinaryData;
import com.azure.core.util.FluxUtil;
import com.cadl.discriminatoredgecases.implementation.DiscriminatorEdgeCasesClientImpl;
import com.cadl.discriminatoredgecases.models.ChildWithAnotherDiscriminator;
import com.cadl.discriminatoredgecases.models.ChildWithRequiredPropertyAsDiscriminator;
import reactor.core.publisher.Mono;

/**
* Initializes a new instance of the asynchronous DiscriminatorEdgeCasesClient type.
*/
@ServiceClient(builder = DiscriminatorEdgeCasesClientBuilder.class, isAsync = true)
public final class DiscriminatorEdgeCasesAsyncClient {
@Generated
private final DiscriminatorEdgeCasesClientImpl serviceClient;

/**
* Initializes an instance of DiscriminatorEdgeCasesAsyncClient class.
*
* @param serviceClient the service client implementation.
*/
@Generated
DiscriminatorEdgeCasesAsyncClient(DiscriminatorEdgeCasesClientImpl serviceClient) {
this.serviceClient = serviceClient;
}

/**
* The getChildRequiredDiscrim operation.
* <p><strong>Response Body Schema</strong></p>
*
* <pre>{@code
* {
* discriminator: String (Required)
* aProperty: String (Required)
* anotherProperty: String (Required)
* }
* }</pre>
*
* @param requestOptions The options to configure the HTTP request before HTTP client sends it.
* @throws HttpResponseException thrown if the request is rejected by server.
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
* @return the response body along with {@link Response} on successful completion of {@link Mono}.
*/
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<BinaryData>> getChildRequiredDiscrimWithResponse(RequestOptions requestOptions) {
return this.serviceClient.getChildRequiredDiscrimWithResponseAsync(requestOptions);
}

/**
* The getChildNewDiscrim operation.
* <p><strong>Response Body Schema</strong></p>
*
* <pre>{@code
* {
* discriminator: String (Required)
* aProperty: String (Required)
* differentDiscriminator: String (Required)
* yetAnotherProperty: String (Required)
* }
* }</pre>
*
* @param requestOptions The options to configure the HTTP request before HTTP client sends it.
* @throws HttpResponseException thrown if the request is rejected by server.
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
* @return the response body along with {@link Response} on successful completion of {@link Mono}.
*/
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<BinaryData>> getChildNewDiscrimWithResponse(RequestOptions requestOptions) {
return this.serviceClient.getChildNewDiscrimWithResponseAsync(requestOptions);
}

/**
* The getChildRequiredDiscrim operation.
*
* @throws HttpResponseException thrown if the request is rejected by server.
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return the response body on successful completion of {@link Mono}.
*/
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<ChildWithRequiredPropertyAsDiscriminator> getChildRequiredDiscrim() {
// Generated convenience method for getChildRequiredDiscrimWithResponse
RequestOptions requestOptions = new RequestOptions();
return getChildRequiredDiscrimWithResponse(requestOptions).flatMap(FluxUtil::toMono)
.map(protocolMethodData -> protocolMethodData.toObject(ChildWithRequiredPropertyAsDiscriminator.class));
}

/**
* The getChildNewDiscrim operation.
*
* @throws HttpResponseException thrown if the request is rejected by server.
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return the response body on successful completion of {@link Mono}.
*/
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<ChildWithAnotherDiscriminator> getChildNewDiscrim() {
// Generated convenience method for getChildNewDiscrimWithResponse
RequestOptions requestOptions = new RequestOptions();
return getChildNewDiscrimWithResponse(requestOptions).flatMap(FluxUtil::toMono)
.map(protocolMethodData -> protocolMethodData.toObject(ChildWithAnotherDiscriminator.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) TypeSpec Code Generator.

package com.cadl.discriminatoredgecases;

import com.azure.core.annotation.Generated;
import com.azure.core.annotation.ReturnType;
import com.azure.core.annotation.ServiceClient;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.exception.ClientAuthenticationException;
import com.azure.core.exception.HttpResponseException;
import com.azure.core.exception.ResourceModifiedException;
import com.azure.core.exception.ResourceNotFoundException;
import com.azure.core.http.rest.RequestOptions;
import com.azure.core.http.rest.Response;
import com.azure.core.util.BinaryData;
import com.cadl.discriminatoredgecases.implementation.DiscriminatorEdgeCasesClientImpl;
import com.cadl.discriminatoredgecases.models.ChildWithAnotherDiscriminator;
import com.cadl.discriminatoredgecases.models.ChildWithRequiredPropertyAsDiscriminator;

/**
* Initializes a new instance of the synchronous DiscriminatorEdgeCasesClient type.
*/
@ServiceClient(builder = DiscriminatorEdgeCasesClientBuilder.class)
public final class DiscriminatorEdgeCasesClient {
@Generated
private final DiscriminatorEdgeCasesClientImpl serviceClient;

/**
* Initializes an instance of DiscriminatorEdgeCasesClient class.
*
* @param serviceClient the service client implementation.
*/
@Generated
DiscriminatorEdgeCasesClient(DiscriminatorEdgeCasesClientImpl serviceClient) {
this.serviceClient = serviceClient;
}

/**
* The getChildRequiredDiscrim operation.
* <p><strong>Response Body Schema</strong></p>
*
* <pre>{@code
* {
* discriminator: String (Required)
* aProperty: String (Required)
* anotherProperty: String (Required)
* }
* }</pre>
*
* @param requestOptions The options to configure the HTTP request before HTTP client sends it.
* @throws HttpResponseException thrown if the request is rejected by server.
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
* @return the response body along with {@link Response}.
*/
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<BinaryData> getChildRequiredDiscrimWithResponse(RequestOptions requestOptions) {
return this.serviceClient.getChildRequiredDiscrimWithResponse(requestOptions);
}

/**
* The getChildNewDiscrim operation.
* <p><strong>Response Body Schema</strong></p>
*
* <pre>{@code
* {
* discriminator: String (Required)
* aProperty: String (Required)
* differentDiscriminator: String (Required)
* yetAnotherProperty: String (Required)
* }
* }</pre>
*
* @param requestOptions The options to configure the HTTP request before HTTP client sends it.
* @throws HttpResponseException thrown if the request is rejected by server.
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
* @return the response body along with {@link Response}.
*/
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<BinaryData> getChildNewDiscrimWithResponse(RequestOptions requestOptions) {
return this.serviceClient.getChildNewDiscrimWithResponse(requestOptions);
}

/**
* The getChildRequiredDiscrim operation.
*
* @throws HttpResponseException thrown if the request is rejected by server.
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return the response.
*/
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
public ChildWithRequiredPropertyAsDiscriminator getChildRequiredDiscrim() {
// Generated convenience method for getChildRequiredDiscrimWithResponse
RequestOptions requestOptions = new RequestOptions();
return getChildRequiredDiscrimWithResponse(requestOptions).getValue()
.toObject(ChildWithRequiredPropertyAsDiscriminator.class);
}

/**
* The getChildNewDiscrim operation.
*
* @throws HttpResponseException thrown if the request is rejected by server.
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return the response.
*/
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
public ChildWithAnotherDiscriminator getChildNewDiscrim() {
// Generated convenience method for getChildNewDiscrimWithResponse
RequestOptions requestOptions = new RequestOptions();
return getChildNewDiscrimWithResponse(requestOptions).getValue().toObject(ChildWithAnotherDiscriminator.class);
}
}
Loading

0 comments on commit c6809aa

Please sign in to comment.