Comments (6)
Hi @NikiforovAll
Thanks for using kiota, and for the great blog post!
The private access modifier is added because the property is marked as readonly in the description.
We generally do not recommend mocking the fluent API and models themselves, but the underlying HTTP request/client.
Is this something you've explored for this scenario?
from kiota.
But such a change would have the same effect as switching from private to public in some scenarios where the client is in the same project as the consuming code from a developer experience perspective.
Let's see what Seb has to say.
from kiota.
Thank you @baywet ,
As shown in the exemplary test, I want to mock IRequestAdapter so I need to create an instance of a class. Unfortunately, NewsTopic
doesn't have required public setters so I can setup various test data.
[Fact]
public async Task TrendingTopic_GetUS_SuccessAsync()
{
// Arrange
var adapter = Substitute.For<IRequestAdapter>();
adapter.SetupSendAsyncWithResponse(new TrendingTopics() { Value = [] });
var newsSearchApiClient = new NewsSearchApiClient(adapter);
// Act
var response = await newsSearchApiClient
.News
.Trendingtopics
.GetAsync(r => r.QueryParameters.Cc = "US");
// Assert
Assert.NotNull(response);
}
I'm not sure if I understand how to properly mock Models in this case.
from kiota.
I'd opt for removing any special treatment of readonly/writeonly properties from Open API spec, and just making all these fields public
. Ideally, the models would be record
s so we could use init
and with
, but I do understand that's not so straightforward. For the time being, public
seems like a fine alternative. I don't see any benefit in restricting those readonly
properties.
In my case, I wanted to test a service that maps API responses (models generated with Kiota) to domain models. I need to be able to mock the Kiota models. It works with Autofixture, I suppose it uses reflection to achieve that, but in some cases it'd be nice to craft some mock manually.
from kiota.
Thank you both for the additional context here.
@NikiforovAll my comment was more about mocking the underlying http response on the http client (used by the request adapter, used in turns by the client).
Generally speaking, the client is "infrastructure code" (c.f. DDD), we recommend wrapping into a service, so it can be mocked, and that service returns object domain models.
The idea of "hiding the setters" for readonly fields was to avoid inducing the client developer in error into thinking a specific field was settable on the API when in fact it's not. Think of it as a developer experience improvement.
If we choose to "remove this feature", which wouldn't be breaking, I'd like to get @sebastienlevert (our PM) input first.
from kiota.
Yes, I understand the general recommendation of wrapping code and it is something that I would generally do, but I think it is still valuable to be able to mock IRequestAdapter
.
I guess another option would be using internal
setters instead of private
since we can make use of InternalsVisibleTo
modification.
from kiota.
Related Issues (20)
- Complete PascalCase transformation for C# clients HOT 1
- Fix scripts to auto update Language and project HOT 1
- Regression Failure because of #4615 HOT 1
- "name_to_be_defined" plugin should be changed to "apiplugin" type
- AllOf implementation alignement
- Error when generating an API plugin HOT 3
- Add "Browse" and the "Default" options when selecting an output directory
- Display the name of the client / plugin being edited on API Explorer
- Enable regeneration after a user generates a client or plugin and without closing the description HOT 1
- CS8625 warnings on generated code
- Remove LINQ usage from the generated code
- [VSCode] It should be possible to load an OpenAPI description from the right-click
- Models not generating for requests with multiple content types HOT 4
- Python attributes are Optional, despite required in OAS schema
- Warnings about discriminator not being inherited
- Entering openapi path with quotes breaks entry HOT 2
- Detect open folder in VS Code is TTK project and change output path for Copilot Plugin HOT 2
- Enhance return types to Include HTTP response details HOT 2
- As a developer, I want to have a sliced OpenAPI document with only the endpoints and components used by my solution when generating a client HOT 1
- Class access modifiers HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kiota.