Azure / autorest.java

Extension for AutoRest (https://github.com/Azure/autorest) that generates Java code
MIT License
33 stars 82 forks source link

[Bug] Generating from swagger with "Value" property in model instead of "value" results in error #2852

Open swathipil opened 4 months ago

swathipil commented 4 months ago

When trying to generate Java code using the swagger emitted from [this tsp], we run into the following error:

error   | Javagen | Failed to generate code.
java.lang.IllegalArgumentException: [JavaCheck/SchemaError] item name value not found among properties of client model SchemaGroups
        at com.azure.autorest.mapper.ClientMethodMapper.getReturnTypes(ClientMethodMapper.java:571)
        at com.azure.autorest.mapper.ClientMethodMapper.createClientMethods(ClientMethodMapper.java:204)
        at com.azure.autorest.mapper.ClientMethodMapper.map(ClientMethodMapper.java:133)
        at com.azure.autorest.mapper.ClientMethodMapper.map(ClientMethodMapper.java:116)
        at com.azure.autorest.mapper.ServiceClientMapper.lambda$processClientOperations$3(ServiceClientMapper.java:177)
        at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:271)
        at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1655)
        at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
        at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
        at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
        at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
        at com.azure.autorest.mapper.ServiceClientMapper.processClientOperations(ServiceClientMapper.java:178)
        at com.azure.autorest.mapper.ServiceClientMapper.map(ServiceClientMapper.java:81)
        at com.azure.autorest.mapper.ClientMapper.map(ClientMapper.java:193)
        at com.azure.autorest.Javagen.processInternal(Javagen.java:83)
        at com.azure.autorest.extension.base.plugin.NewPlugin.process(NewPlugin.java:338)
        at com.azure.autorest.Main.lambda$main$1(Main.java:15)
        at com.azure.autorest.extension.base.jsonrpc.Connection.lambda$dispatch$2(Connection.java:163)
        at com.azure.autorest.extension.base.jsonrpc.Connection.lambda$process$3(Connection.java:262)
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:829)

fatal   | Process() cancelled due to failure 
error   |   Error: Plugin javagen reported failure.
error   | Autorest completed with an error. If you think the error message is unclear, or is a bug, please declare an issues at 

This is because autorest for Java seems to expect that the property be called "value" rather than "Value" in the SchemaGroups/SchemaVersions definitions. However, the Schema Registry service returns "Value". For example:

{'NextLink': 'https://namespace.servicebus.windows.net/$schemaGroups/azsdk_python_test_group/schemas/your-schema-name/versions?api-version=2023-07-01&%24skip=5', 'Value': [1, 2, 3, 4, 5, 6]}

This seems to be a bug in autorest.java, as it works with Python and the swagger matches service behavior.

weidongxu-microsoft commented 4 months ago

@swathipil

This appears to be bug from typespec-autorest, which is used to convert the TypeSpec to Swagger.

Here in Swagger https://github.com/Azure/azure-rest-api-specs/blob/main/specification/schemaregistry/data-plane/Microsoft.SchemaRegistry/stable/2023-07-01/schemaregistry.json#L91-L93

        "x-ms-pageable": {
          "nextLinkName": "NextLink"
        }

It should include the itemName: Value as well see https://github.com/Azure/autorest/blob/main/docs/extensions/readme.md#x-ms-pageable on itemName

You can move this issue to https://github.com/Azure/typespec-azure/issues, which contains typespec-autorest lib

weidongxu-microsoft commented 4 months ago

Here is one of your Swagger, prior typespec https://github.com/Azure/azure-rest-api-specs/blob/616302e10e5ce0f80d2f0eaf8002f3e39d033696/specification/schemaregistry/data-plane/Microsoft.EventHub/stable/2023-07-01/schemaregistry.json#L49-L52

You can see

        "x-ms-pageable": {
          "nextLinkName": "nextLink",
          "itemName": "schemaGroups"
        },

    "SchemaGroups": {
      "type": "object",
      "description": "Object received from the registry containing the list of schema groups and link to next batch page.",
      "properties": {
        "schemaGroups": {
          "type": "array",
          "description": "Array of schema groups.",
          "items": {
            "$ref": "#/definitions/SchemaGroup"
          },
          "x-ms-client-name": "groups"
        },
        "nextLink": {
          "type": "string",
          "description": "URl to next batch of schema groups"
        }
      }
    },

The value of itemName matchs the schemaGroups property in SchemaGroups.

weidongxu-microsoft commented 4 months ago

BTW, I am not sure why schemaGroups changed to Value, nextLink changed to NextLink in same api-version?

swathipil commented 4 months ago

Hi @weidongxu-microsoft - That was a bug in the swagger before, but the list operations are not part of the public API for the SDKs so we never tested them. I recently converted the swagger to typespec and tested these operations as well, and found that the service was returning Value/NextLink rather than schemaGroups/nextLink. "Value"/"NextLink" are correct.

weidongxu-microsoft commented 4 months ago

Got it.

Then the question is still on typespec-autorest about its missing

          "itemName": "Value"

when convert TypeSpec to Swagger.

Please raise it on their repo https://github.com/Azure/typespec-azure/issues


Meanwhile, you may have to use a directive in readme.md to add that "itemName": "Value" into the autorest procedure, to avoid failure.

swathipil commented 4 months ago

@weidongxu-microsoft - This error doesn't happen during the conversion, and we're able to successfully generate from the tsp, which also has "Value" rather than "value". This error happens when we run autorest.java with the swagger, not the typespec. So I believe this is a problem with generating from swagger, and not the typespec to swagger converter.