Validate Requests in NSwag generated Client - c#

I have a .NET 6 Webclient and REST Contract, that is generated from a YAML with NSwag.
The contract contains some validation properties. Is there any way to validate my request on the client side? I don't want to write the validation code by hand.
In the NSwag documentation I only found flags to generate validation attributes for generated controllers, but not for the web client.
YAML:
- name: anyField
in: query
description: Field with max value=20 and required
required: true
schema:
maximum: 20
type: integer
format: int32
Generated contract code:
/// <summary>
/// Field with max value=20 and required
/// </summary>
[Newtonsoft.Json.JsonProperty("anyField", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public int AnyField{ get; set; }

I could read the yaml with NSwag, get the json schema and use this for the validation:
public async Task<ICollection<ValidationError>> Validate(Request request) {
var yamlBytes = resx.yaml;
await using var yamlMs = new MemoryStream(yamlBytes);
using var yamlReader = new StreamReader(yamlMs);
var yamlText = await yamlReader.ReadToEndAsync();
var apiDocument = await NSwag.OpenApiYamlDocument.FromYamlAsync(yamlText);
var schema = apiDocument.Paths["/restPath"]["post"].RequestBody.Content["application/json"].Schema;
var jsonSettings = new JsonSettingsProvider().ProvideSettings;
var body = JsonConvert.SerializeObject(request, jsonSettings);
var errors = schema.Validate(body);
return errors;
}

Related

C# HTTP PATCH using HTTPClient

I've written a test using Test Server in dot net core 3.1 and I'm trying to do a PATCH request to an endpoint. However as I'm new to using PATCH, I'm a bit stuck with how to send the correct object that the endpoint is expecting.
[Fact]
public async Task Patch()
{
var operations = new List<Operation>
{
new Operation("replace", "entryId", "'attendance ui", 5)
};
var jsonPatchDocument = new JsonPatchDocument(operations, new DefaultContractResolver());
// Act
var content = new StringContent(JsonConvert.SerializeObject(jsonPatchDocument), Encoding.UTF8, "application/json");
var httpResponse = await HttpClient.PatchAsync($"v1/Entry/1", content);
var actual = await httpResponse.Content.ReadAsStringAsync();
}
[HttpPatch("{entryId}")]
public async Task<ActionResult> Patch(int entryId, [FromBody] JsonPatchDocument<EntryModel> patchDocument)
{
if (patchDocument == null)
{
return BadRequest();
}
var existingEntry = _mapper.Map<EntryModel>(await _entryService.Get(entryId));
patchDocument.ApplyTo(existingEntry);
var entry = _mapper.Map<Entry>(existingEntry);
var updatedEntry = _mapper.Map<Entry>(await _entryService.Update(entryId, entry));
return Ok(await updatedEntry.ModelToPayload());
}
From the example I'm creating a JsonPatchDocument with a list of operations, serializing it to JSON and then doing PatchAsync with HTTP Client with the URL for the endpoint.
So my question is what is the shape of the object that I should be Patching and I'm doing this correctly in general?
I tried sending the EntryModel as shown in the picture below, however patchDocument.Operations has an empty list.
Thanks,
Nick
I ended up solving my problem by doing several things:
JsonPatchDocument doesn't seem to work without the dependency
services.AddControllers().AddNewtonsoftJson(); in Startup.cs. This is from the Nuget package `Microsoft.AspNetCore.Mvc.Newtonsoft.json.
There is an easier way to create the array than the answer from #Neil. Which is this: var patchDoc = new JsonPatchDocument<EntryModel>().Replace(o => o.EntryTypeId, 5);
You need this specific media type:
var content = new StringContent(JsonConvert.SerializeObject(patchDoc), Encoding.UTF8, "application/json-patch+json");
Here is the complete code:
/// <summary>
/// Verify PUT /Entrys is working and returns updated records
/// </summary>
[Fact]
public async Task Patch()
{
var patchDoc = new JsonPatchDocument<EntryModel>()
.Replace(o => o.EntryTypeId, 5);
var content = new StringContent(JsonConvert.SerializeObject(patchDoc), Encoding.UTF8, "application/json-patch+json");
var httpResponse = await HttpClient.PatchAsync($"v1/Entry/1", content);
var actual = await httpResponse.Content.ReadAsStringAsync();
// Assert
Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode);
Assert.True(httpResponse.IsSuccessStatusCode);
}
/// <summary>
/// Endpoint to do partial update
/// </summary>
/// <returns></returns>
[HttpPatch("{entryId}")]
public async Task<ActionResult> Patch(int entryId, [FromBody] JsonPatchDocument<EntryModel> patchDocument)
{
if (patchDocument == null)
{
return BadRequest();
}
var existingEntry = _mapper.Map<EntryModel>(await _entryService.Get(entryId));
// Apply changes
patchDocument.ApplyTo(existingEntry);
var entry = _mapper.Map<Entry>(existingEntry);
var updatedEntry = _mapper.Map<Entry>(await _entryService.Update(entryId, entry));
return Ok();
}
Take a look at this page: https://learn.microsoft.com/en-us/aspnet/core/web-api/jsonpatch?view=aspnetcore-3.1
But the content is something like:
[ {
"op": "add",
"path": "/customerName",
"value": "Barry" }, {
"op": "add",
"path": "/orders/-",
"value": {
"orderName": "Order2",
"orderType": null
} } ]
The JsonPatchDocument will not work. To make it work you have to add a media type formater.
Install Microsoft.AspNetCore.Mvc.NewtonsoftJson
In startup.cs , after AddControllers() add ->
.AddNewtonsoftJson(x =>
x.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver(); })
If you need to have JSON as the default media type formatter. Keep it before any other media type of formatter.

Error "405 Method Not Allow" When Calling Put method in Postman with body parameter

I was trying to call the Put method through Postman and always getting error: "405 Method Not Allow" and "Message": "The requested resource does not support http method 'PUT'."
I'm using DocumentDB and C#. Here is my code:
[Route("multilanguage/Resources/{id}/{Language}")]
[HttpPut]
public async Task<IHttpActionResult> UpdateResource(string Id, string Language, string text)
{
client = new DocumentClient(new Uri(EndPoint), AuthKey);
var collectionLink = UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId);
var query = new SqlQuerySpec("SELECT * FROM MultiLanguage as m where m.id = #pmId",
new SqlParameterCollection(new SqlParameter[] { new SqlParameter { Name = "#pmId", Value = Id } }));
Document doc = client.CreateDocumentQuery<Document>(
collectionLink, query).AsEnumerable().FirstOrDefault();
List<Models.Translations> d = doc.GetPropertyValue<List<Models.Translations>>("Translations");
Models.Translations temp = d.Find(p => p.Language == Language);
temp.Content = text;
temp.LastModified = DateTimeOffset.Now;
temp.ModifiedBy = "admin";
doc.SetPropertyValue("Translations", d);
Document updated = await client.ReplaceDocumentAsync(doc);
return Ok();
}
When I call the Put method throught Postman, I call "http://localhost:XXXX/multilanguage/resources/2/En". "2" and "En" are the first two parameters in my code. And I also specify the "text" parameter value in the Postman request Body with x-www-form-urlencoded type: key = text, value = Test! This put method suppose to update the temp.Content value to "Test!". However, it always failed with the error I mentioned above.
Did I miss anything here?
The 405 error when performing a PUT request to web api is a well known topic. You can find many solutions in this or this SO question.
And for the design of you controller:
PUT are designed to have a body, just like POST and in your case
you should send all parameters in the body instead.
You should create a class which contains the objects you want to send to the server:
public class resourceClass
{
public string Id { get; set; }
public string Language { get; set; }
public string text { get; set; }
}
Then specify the route without the attribute routing and get the object from the request body
[Route("multilanguage/Resources/PutResource")]
[HttpPut]
public async Task<IHttpActionResult> UpdateResource([FromBody] resourceClass obj)
{
client = new DocumentClient(new Uri(EndPoint), AuthKey);
var collectionLink = UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId);
var query = new SqlQuerySpec("SELECT * FROM MultiLanguage as m where m.id = #pmId",
new SqlParameterCollection(new SqlParameter[] { new SqlParameter { Name = "#pmId", Value = Id } }));
Document doc = client.CreateDocumentQuery<Document>(
collectionLink, query).AsEnumerable().FirstOrDefault();
List<Models.Translations> d = doc.GetPropertyValue<List<Models.Translations>>("Translations");
Models.Translations temp = d.Find(p => p.Language == Language);
temp.Content = text;
temp.LastModified = DateTimeOffset.Now;
temp.ModifiedBy = "admin";
doc.SetPropertyValue("Translations", d);
Document updated = await client.ReplaceDocumentAsync(doc);
return Ok();
}
From the client you could add an object to the PUT request of Content-Type application/json like this
var data = {
Id: clientId,
Language: clientLanguage,
text: clientText
};
Don't forget to stringify the json when adding it to the http request
data: JSON.stringify(data),
The PUT controller will then be reached at "http://localhost:XXXX/multilanguage/resources/putresource".
Check the URL for which you are posting the data, in my case the URL was incorrect because of which I got these errors, also verify that in Body you should select raw and change the Text to JSON if you are passing a JSON as a data.

Data Validation Master Data Service

I'm currently working on a POC MDS/MDM WCF service and have a question regarding validation. Does anyone have an example of calling the MDS web api to kick of validating the MDS model? I know i have to add a service reference to MDS in order to gain access to the proxies, i was just hoping for a simple example of using the api.
https://msdn.microsoft.com/en-us/library/microsoft.masterdataservices.serviceclient.validationprocess(v=sql.110).aspx
//ValidationProcess For an entity
public Collection<ValidationIssue> ValidationProcess(string ModelName, string verName, string EntityName, string memCode)
{
//Instantiate all of request and response objects
ValidationProcessRequest Request = new ValidationProcessRequest();
ValidationProcessResponse Response = new ValidationProcessResponse();
//Instantiate the Criteria and Options objects
Request.ValidationProcessCriteria = new ValidationProcessCriteria();
Request.ValidationProcessOptions = new ValidationProcessOptions();
//Set Model and Version Identifiers - these will be required in all instances
Request.ValidationProcessCriteria.ModelId = new Identifier { Name = ModelName };
Request.ValidationProcessCriteria.VersionId = new Identifier { Name = verName };
Request.ValidationProcessCriteria.EntityId = new Identifier { Name = EntityName };
Request.ValidationProcessCriteria.Members = new Collection<MemberIdentifier>();
Request.ValidationProcessCriteria.Members.Add(new MemberIdentifier { Code = memCode });
//Options can return validation results or trigger the commit of a version (when validation is already successful)
Request.ValidationProcessOptions.ReturnValidationResults = true;
Response = mds_Proxy.ValidationProcess(Request);
return Response.ValidationIssueList;
}

Web Api json response $id variable

in asp web api i have this controller that respond to ajax request and send back json data:
public IEnumerable<PersonaleDTO> GetPersonale()
{
var n = (from p in db.Personale
select new PersonaleDTO { Id = p.Id, Nome = p.Nome, Cognome = p.Cognome Cellulare = p.Cellulare, Email = p.Email, Attivo = (bool)p.Attivo }).ToList();
return n;
}
This method return seven object and it's correct.
Now when i receive back the data in the json format i see that i receive also and $id member along with the id, Nome, Cognome...
What is the $id variable? How can remove this from the json response?
Try this code in WebApiConfig
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
Edit : If for some reason you're using a custom ContractResolver then as per this post
The custom ContractResolver setting overrides the PreserveReferencesHandling setting.
In your implementation of DefaultContractResolver/IContractResolver, add this;
public override JsonContract ResolveContract(Type type) {
var contract = base.ResolveContract(type);
contract.IsReference = false;
return contract;
}
This behaves similarly to the PreserveReferencesHandling.None setting without a custom ContractResolver.

Adding and Retrieving data from request context

I'm trying to attach an api key to the OperationContext outgoing message header as follows:
public static void AddApikeyToHeader(string apikey, IContextChannel channel, string address)
{
using (OperationContextScope scope = new OperationContextScope(channel))
{
MessageHeader header = MessageHeader.CreateHeader("apikey", address, apikey);
OperationContext.Current.OutgoingMessageHeaders.Add(header);
}
}
but then I have no idea how to retrieve the header on the server side. I'm using a Service authorisation manager and I get the current operating context and try to retrieve the header like this:
public string GetApiKey(OperationContext operationContext)
{
var request = operationContext.RequestContext.RequestMessage;
var prop = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
return prop.Headers["apikey"];
}
but there is no apikey header attached there. Also, on debugging when I inspect the operationContext I cant seem to see my apikey header anywhere. Can anyone see where I'm going wrong?
You can add custom header by this way :
using (ChannelFactory<IMyServiceChannel> factory =
new ChannelFactory<IMyServiceChannel>(new NetTcpBinding()))
{
using (IMyServiceChannel proxy = factory.CreateChannel(...))
{
using ( OperationContextScope scope = new OperationContextScope(proxy) )
{
Guid apiKey = Guid.NewGuid();
MessageHeader<Guid> mhg = new MessageHeader<Guid>(apiKey);
MessageHeader untyped = mhg.GetUntypedHeader("apiKey", "ns");
OperationContext.Current.OutgoingMessageHeaders.Add(untyped);
proxy.DoOperation(...);
}
}
}
And service side, you can get header like :
Guid apiKey =
OperationContext.Current.IncomingMessageHeaders.GetHeader<Guid>("apiKey", "ns");
I'm assuming that you trying to consume your service using some Http Protocol based transport (SOAP, REST etc). I'm also assuming that what you want is to authorize the caller using the supplied API key. If both of those conditions apply to your question, you can read on.
I recently had to tackle a similar problem only that I did not pass an API key but a username/password hash combination using some HTTP custom headers. I ultimately solved it by implementing a custom authorization policy that once configured in Web.config hooked nicely into the WCF Pipeline.
The snippet below should be enough to get you started. You probably would have to replace the x-ms-credentials-XXX headers by a single one representing your API key.
internal class RESTAuthorizationPolicy : IAuthorizationPolicy
{
public RESTAuthorizationPolicy()
{
Id = Guid.NewGuid().ToString();
Issuer = ClaimSet.System;
}
public bool Evaluate(EvaluationContext evaluationContext, ref object state)
{
const String HttpRequestKey = "httpRequest";
const String UsernameHeaderKey = "x-ms-credentials-username";
const String PasswordHeaderKey = "x-ms-credentials-password";
const String IdentitiesKey = "Identities";
const String PrincipalKey = "Principal";
// Check if the properties of the context has the identities list
if (evaluationContext.Properties.Count > 0 ||
evaluationContext.Properties.ContainsKey(IdentitiesKey) ||
!OperationContext.Current.IncomingMessageProperties.ContainsKey(HttpRequestKey))
return false;
// get http request
var httpRequest = (HttpRequestMessageProperty)OperationContext.Current.IncomingMessageProperties[HttpRequestKey];
// extract credentials
var username = httpRequest.Headers[UsernameHeaderKey];
var password = httpRequest.Headers[PasswordHeaderKey];
// verify credentials complete
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
return false;
// Get or create the identities list
if (!evaluationContext.Properties.ContainsKey(IdentitiesKey))
evaluationContext.Properties[IdentitiesKey] = new List<IIdentity>();
var identities = (List<IIdentity>) evaluationContext.Properties[IdentitiesKey];
// lookup user
using (var con = ServiceLocator.Current.GetInstance<IDbConnection>())
{
using (var userDao = ServiceLocator.Current.GetDao<IUserDao>(con))
{
var user = userDao.GetUserByUsernamePassword(username, password);
...
Did you take a look at this question: How to add a custom HTTP header to every WCF call? ? It may contain your solution.

Categories