Skip to content
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
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/http-client-java"
---

Fix discriminator property not generated when model has @discriminator but no known subtypes
10 changes: 6 additions & 4 deletions packages/http-client-java/emitter/src/code-model-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2829,13 +2829,15 @@ export class CodeModelBuilder {
}

// discriminator
if (type.discriminatedSubtypes && type.discriminatorProperty) {
if (type.discriminatorProperty) {
objectSchema.discriminator = new Discriminator(
this.processModelProperty(type.discriminatorProperty),
);
for (const discriminatorValue in type.discriminatedSubtypes) {
const subType = type.discriminatedSubtypes[discriminatorValue];
this.processSchema(subType, subType.name);
if (type.discriminatedSubtypes) {
for (const discriminatorValue in type.discriminatedSubtypes) {
const subType = type.discriminatedSubtypes[discriminatorValue];
this.processSchema(subType, subType.name);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import tsptest.discriminatoredgecases.implementation.DiscriminatorEdgeCasesClientImpl;
import tsptest.discriminatoredgecases.models.ChildWithAnotherDiscriminator;
import tsptest.discriminatoredgecases.models.ChildWithRequiredPropertyAsDiscriminator;
import tsptest.discriminatoredgecases.models.ModelWithDiscriminatorNoSubtypes;

/**
* Initializes a new instance of the asynchronous DiscriminatorEdgeCasesClient type.
Expand Down Expand Up @@ -94,6 +95,32 @@ public Mono<Response<BinaryData>> getChildNewDiscrimWithResponse(RequestOptions
return this.serviceClient.getChildNewDiscrimWithResponseAsync(requestOptions);
}

/**
* The getNoSubtypes operation.
* <p><strong>Response Body Schema</strong></p>
*
* <pre>
* {@code
* {
* kind: String (Required)
* name: 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 model with along with {@link Response} on successful completion of {@link Mono}.
*/
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<BinaryData>> getNoSubtypesWithResponse(RequestOptions requestOptions) {
return this.serviceClient.getNoSubtypesWithResponseAsync(requestOptions);
}

/**
* The getChildRequiredDiscrim operation.
*
Expand Down Expand Up @@ -131,4 +158,23 @@ public Mono<ChildWithAnotherDiscriminator> getChildNewDiscrim() {
return getChildNewDiscrimWithResponse(requestOptions).flatMap(FluxUtil::toMono)
.map(protocolMethodData -> protocolMethodData.toObject(ChildWithAnotherDiscriminator.class));
}

/**
* The getNoSubtypes 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 model with on successful completion of {@link Mono}.
*/
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<ModelWithDiscriminatorNoSubtypes> getNoSubtypes() {
// Generated convenience method for getNoSubtypesWithResponse
RequestOptions requestOptions = new RequestOptions();
return getNoSubtypesWithResponse(requestOptions).flatMap(FluxUtil::toMono)
.map(protocolMethodData -> protocolMethodData.toObject(ModelWithDiscriminatorNoSubtypes.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import tsptest.discriminatoredgecases.implementation.DiscriminatorEdgeCasesClientImpl;
import tsptest.discriminatoredgecases.models.ChildWithAnotherDiscriminator;
import tsptest.discriminatoredgecases.models.ChildWithRequiredPropertyAsDiscriminator;
import tsptest.discriminatoredgecases.models.ModelWithDiscriminatorNoSubtypes;

/**
* Initializes a new instance of the synchronous DiscriminatorEdgeCasesClient type.
Expand Down Expand Up @@ -92,6 +93,32 @@ public Response<BinaryData> getChildNewDiscrimWithResponse(RequestOptions reques
return this.serviceClient.getChildNewDiscrimWithResponse(requestOptions);
}

/**
* The getNoSubtypes operation.
* <p><strong>Response Body Schema</strong></p>
*
* <pre>
* {@code
* {
* kind: String (Required)
* name: 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 model with along with {@link Response}.
*/
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<BinaryData> getNoSubtypesWithResponse(RequestOptions requestOptions) {
return this.serviceClient.getNoSubtypesWithResponse(requestOptions);
}

/**
* The getChildRequiredDiscrim operation.
*
Expand Down Expand Up @@ -128,4 +155,22 @@ public ChildWithAnotherDiscriminator getChildNewDiscrim() {
RequestOptions requestOptions = new RequestOptions();
return getChildNewDiscrimWithResponse(requestOptions).getValue().toObject(ChildWithAnotherDiscriminator.class);
}

/**
* The getNoSubtypes 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 model with.
*/
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
public ModelWithDiscriminatorNoSubtypes getNoSubtypes() {
// Generated convenience method for getNoSubtypesWithResponse
RequestOptions requestOptions = new RequestOptions();
return getNoSubtypesWithResponse(requestOptions).getValue().toObject(ModelWithDiscriminatorNoSubtypes.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,24 @@ Mono<Response<BinaryData>> getChildNewDiscrim(@HostParam("endpoint") String endp
@UnexpectedResponseExceptionType(HttpResponseException.class)
Response<BinaryData> getChildNewDiscrimSync(@HostParam("endpoint") String endpoint,
@HeaderParam("Accept") String accept, RequestOptions requestOptions, Context context);

@Get("/model/nosubtypes")
@ExpectedResponses({ 200 })
@UnexpectedResponseExceptionType(value = ClientAuthenticationException.class, code = { 401 })
@UnexpectedResponseExceptionType(value = ResourceNotFoundException.class, code = { 404 })
@UnexpectedResponseExceptionType(value = ResourceModifiedException.class, code = { 409 })
@UnexpectedResponseExceptionType(HttpResponseException.class)
Mono<Response<BinaryData>> getNoSubtypes(@HostParam("endpoint") String endpoint,
@HeaderParam("Accept") String accept, RequestOptions requestOptions, Context context);

@Get("/model/nosubtypes")
@ExpectedResponses({ 200 })
@UnexpectedResponseExceptionType(value = ClientAuthenticationException.class, code = { 401 })
@UnexpectedResponseExceptionType(value = ResourceNotFoundException.class, code = { 404 })
@UnexpectedResponseExceptionType(value = ResourceModifiedException.class, code = { 409 })
@UnexpectedResponseExceptionType(HttpResponseException.class)
Response<BinaryData> getNoSubtypesSync(@HostParam("endpoint") String endpoint,
@HeaderParam("Accept") String accept, RequestOptions requestOptions, Context context);
}

/**
Expand Down Expand Up @@ -273,4 +291,57 @@ public Response<BinaryData> getChildNewDiscrimWithResponse(RequestOptions reques
final String accept = "application/json";
return service.getChildNewDiscrimSync(this.getEndpoint(), accept, requestOptions, Context.NONE);
}

/**
* The getNoSubtypes operation.
* <p><strong>Response Body Schema</strong></p>
*
* <pre>
* {@code
* {
* kind: String (Required)
* name: 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 model with along with {@link Response} on successful completion of {@link Mono}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<BinaryData>> getNoSubtypesWithResponseAsync(RequestOptions requestOptions) {
final String accept = "application/json";
return FluxUtil
.withContext(context -> service.getNoSubtypes(this.getEndpoint(), accept, requestOptions, context));
}

/**
* The getNoSubtypes operation.
* <p><strong>Response Body Schema</strong></p>
*
* <pre>
* {@code
* {
* kind: String (Required)
* name: 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 model with along with {@link Response}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<BinaryData> getNoSubtypesWithResponse(RequestOptions requestOptions) {
final String accept = "application/json";
return service.getNoSubtypesSync(this.getEndpoint(), accept, requestOptions, Context.NONE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) TypeSpec Code Generator.

package tsptest.discriminatoredgecases.models;

import com.azure.core.annotation.Generated;
import com.azure.core.annotation.Immutable;
import com.azure.json.JsonReader;
import com.azure.json.JsonSerializable;
import com.azure.json.JsonToken;
import com.azure.json.JsonWriter;
import java.io.IOException;

/**
* Model with.
*/
@Immutable
public final class ModelWithDiscriminatorNoSubtypes implements JsonSerializable<ModelWithDiscriminatorNoSubtypes> {
/*
* The kind property.
*/
@Generated
private String kind = "ModelWithDiscriminatorNoSubtypes";

/*
* The name property.
*/
@Generated
private final String name;

/**
* Creates an instance of ModelWithDiscriminatorNoSubtypes class.
*
* @param name the name value to set.
*/
@Generated
private ModelWithDiscriminatorNoSubtypes(String name) {
this.name = name;
}

/**
* Get the kind property: The kind property.
*
* @return the kind value.
*/
@Generated
public String getKind() {
return this.kind;
}

/**
* Get the name property: The name property.
*
* @return the name value.
*/
@Generated
public String getName() {
return this.name;
}

/**
* {@inheritDoc}
*/
@Generated
@Override
public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
jsonWriter.writeStartObject();
jsonWriter.writeStringField("name", this.name);
jsonWriter.writeStringField("kind", this.kind);
return jsonWriter.writeEndObject();
}

/**
* Reads an instance of ModelWithDiscriminatorNoSubtypes from the JsonReader.
*
* @param jsonReader The JsonReader being read.
* @return An instance of ModelWithDiscriminatorNoSubtypes if the JsonReader was pointing to an instance of it, or
* null if it was pointing to JSON null.
* @throws IllegalStateException If the deserialized JSON object was missing any required properties.
* @throws IOException If an error occurs while reading the ModelWithDiscriminatorNoSubtypes.
*/
@Generated
public static ModelWithDiscriminatorNoSubtypes fromJson(JsonReader jsonReader) throws IOException {
return jsonReader.readObject(reader -> {
String name = null;
String kind = null;
while (reader.nextToken() != JsonToken.END_OBJECT) {
String fieldName = reader.getFieldName();
reader.nextToken();

if ("name".equals(fieldName)) {
name = reader.getString();
} else if ("kind".equals(fieldName)) {
kind = reader.getString();
} else {
reader.skipChildren();
}
}
ModelWithDiscriminatorNoSubtypes deserializedModelWithDiscriminatorNoSubtypes
= new ModelWithDiscriminatorNoSubtypes(name);
deserializedModelWithDiscriminatorNoSubtypes.kind = kind;

return deserializedModelWithDiscriminatorNoSubtypes;
});
}
}
Loading
Loading