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

Typespec python generator doesn’t consider namespaces #2435

Closed
cdrlseg opened this issue Mar 4, 2024 · 5 comments
Closed

Typespec python generator doesn’t consider namespaces #2435

cdrlseg opened this issue Mar 4, 2024 · 5 comments
Labels
customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that

Comments

@cdrlseg
Copy link

cdrlseg commented Mar 4, 2024

We would like to be able to generate python Api code that takes into account the namespaces. This is useful for parameters and function name simplification in a global API context.
We use TypeSpec compiler v0.53.1

Sample description:

  1. We want to define an API that contains two different classes: classA1 and class A2 each class defined in their own namespace.
  2. Each class defines two operations (nameOperation1 and nameOperation2) with the same name but with different route path (see apis.tsp files)
  3. Each operation is a customOperationCollection with an operation request and response, having the same name (see api-models.tsp)

--> Typespec-ts manage to generate the client.
--> Typespec-python generates the following error:

File "C:\Dev\zen-spec\node_modules@autorest\python\autorest\codegen_init_.py", line 467, in
).process()
^^^^^^^^^
File "C:\Dev\zen-spec\node_modules@autorest\python\autorest\codegen_init_.py", line 349, in process
code_model = CodeModel(yaml_data=yaml_data, options=options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Dev\zen-spec\node_modules@autorest\python\autorest\codegen\models\code_model.py", line 73, in init
self.sort_model_types()
File "C:\Dev\zen-spec\node_modules@autorest\python\autorest\codegen\models\code_model.py", line 255, in sort_model_types
self._sort_model_types_helper(
File "C:\Dev\zen-spec\node_modules@autorest\python\autorest\codegen\models\code_model.py", line 224, in _sort_model_types_helper
raise ValueError(
ValueError: We have already generated a schema with name Operation1Request

We would like to be able to have code generated for every operation – this can be done by creation a _models.py per namespace or by adding the namespace into the method name.

@microsoft-github-policy-service microsoft-github-policy-service bot added customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Mar 4, 2024
@iscai-msft
Copy link
Contributor

@cdrlseg we are able to generate operations in different namespaces, but the design of the python SDK is to have all of the models defined in the models subnamespace. What seems to be happening here is you have an anonymous model (a model defined inline in an operation) for both of your operations in each namesapce. I would recommend just defining one model in this case that both operations will use.

I believe this would also be better typespec definition code so you don't generate duplicate models when not needed

@cdrlseg
Copy link
Author

cdrlseg commented Mar 5, 2024

Hi @iscai-msft . I don't know if you opened the attached sample. In our case we would like to be able to define two models that have the same name but not the same signature, each model is in a separate namespace.
Here we have operation1Request // operation2Request as well as operation1Response//operation2Response with same name but in different namespaces (classA1 and classA2). Or the generator do not accept this behavior as the namespace is not taken into account. As the generated methods have the same name the function sort_model_types() returns an error.

Below the code extraction:

namespace classA1{

using TypeSpec.Http;
using TypeSpec.JsonSchema;

model operation1Request {
parameters1?: string;
parameters2?: string;
};

model operation2Request {
parameters1?: string;
parameters2?: string;
};

model operation1Response {
parameters1?: string;
parameters2?: string;
parameters3?: string;
};

model operation2Response {
parameters1?: string;
parameters2?: string;
parameters3?: string;
};
}

=======================

namespace classA2{
using TypeSpec.Http;
using TypeSpec.JsonSchema;

model operation1Request {
parameters1?: string;
parameters2?: string;
parameters3?: string;
parameters4?: string;
};

model operation2Request {
parameters1?: string;
parameters2?: string;
};

model operation1Response {
parameters1?: string;
parameters2?: string;
parameters3?: string;
};

model operation2Response {
parameters1?: string;
parameters2?: string;
parameters3?: string;
};
}

@iscai-msft
Copy link
Contributor

iscai-msft commented Mar 5, 2024

I see, so you had purposefully designed two models called Operation1Request in 2 separate namespaces. Python still doesn't support models in different namespaces, we only generate them in the .models namespace, so this does result in a name conflict error.

I would still caution against this though, because code users can very easily run into model name conflicts when they're using your generated sdks. There are also a lot of languages that don't support this as well.

Let me forward this issue to our Python code architect though @johanste

@johanste
Copy link
Member

johanste commented Mar 5, 2024

Currently, our generators do not support generating things into separate modules/packages/namespaces. We have a feature on the backlog to override the namespace for a given model.

The main reason that it has not been prioritized is that we/azure services avoid using the same model name for any given service due to the possibilities of confusion that @iscai-msft called out. In other words, it has not popped up as an urgent need.

Can you elaborate a bit on the use case you have? Is there a specific scenario where having the same name is strongly preferred?

@cdrlseg
Copy link
Author

cdrlseg commented Mar 6, 2024

I can send to you our typespec APi definition if you need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that
Projects
None yet
Development

No branches or pull requests

3 participants