How do you know if a value was passed in on a property that does not have the [Required] flag.
What will be the value of an string that is not required and was not passed in? If it is an empty string then how do you know the difference from a empty string sent by the caller?
If you need to know if a value was set or not then you can make a flag in your property for example
public MyTask : Task
{
private string mName;
private bool mNameSet;
public string Name
{
get{return mName;}
set
{
mName = value;
mNameSet = true;
}
}
... MORE HERE
}
So you can just check the mNameSet flag to see if the property was set or not.
Sayed Ibrahim Hashimi
My Book: Inside the Microsoft Build Engine : Using MSBuild and Team Foundation Build
You can't tell the difference. Both will be null if the task doesnt set a default value in the task constructor.
I don't know if it should make a difference to the custom task. If a parameter is null or empty --- String.IsNullOrEmpty() --- then the task should branch into the default logic for that particular value.
Related
I must be missing something out in .NET 6.
This same code worked perfectly fine in previous .NET core. I've found a work arround, but please I need someone to explain to me what's going wrong.
I have a settings class with its JSON representation in my appsettings.json
public class MongoSettings
{
public string ConnectionString { get; set; }
public string DatabaseName { get; set; }
public MongoSettings()
{
ConnectionString = string.Empty;
DatabaseName = string.Empty;
}
}
According to what I read in this documentation, I should add the settings this way:
builder.Services.Configure<MongoSettings>(c => builder.Configuration.GetSection("MongoSettings"));
And I did just that.
I inject this setting as an IOption inside an object I registered in my DI container.
This is the constructor that will receive the settings.
public MongoConnection(IOptions<MongoSettings> option)
The problem is that the object I receive in the settings always has default values.
My settings are present in the configuration appsettings.json I know that because, when I do the following, I get my settings with its value set to what is in my settings json file:
var mongoSettings = Configuration.GetSection("MongoSettings").Get<MongoSettings>();
My question is: Why is the IOptions object I get in my constructor always default ?
This used to work in prior .net versions and in the docs they clearly state that it is the right way.
I am using [FromQuery] atribute in controller's Get method:
//CarsController, etc..
[HttpGet]
public async Task<ActionResult<IEnumerable<CarsDto>>> Get([FromQuery] CarsParameter? carsParam = null)
{
//param is always not null here
}
Inside the method I need to distinguish between api/cars and api/cars?color=red calls. Problem is, that carsParam object is never null, so I cannot say if the Color="" (defailt value) is intended to be empty string or it's because of the call was api/cars
CarsParameter is a simple class:
public class CarsParameter
{
public string Color {get; set;} = "";
//more params here
}
Yes, I can use different path, like api/cars/withParams?color=red, but i am looking for more subtle solution.
I need to distinguish between api/cars and api/cars?color=red calls. Problem is, that carsParam object is never null
Please note that default model binding starts by looking through the sources for the key carsParam.Color. If that isn't found, it looks for Color without a prefix, which cause the issue.
To achieve your requirement, you can try to specify prefix explicitly, like below.
public async Task<ActionResult<IEnumerable<CarsDto>>> Get([FromQuery][Bind(Prefix = "carsParam")] CarsParameter? carsParam = null)
{
Request to api/cars?color=red&carsParam.color=yellow&carsParam.brand=test and following is test result
I am trying to fetch a property from the properties file in java code written in mule.
Class Example {
#NotBlank("message" = "${prop1}")
String key1;
String key2;
}
prop1 is a property stored in properties file
prop1 = " 001 | key1 cannot be blank"
I want prop1 to be resolved as 001 | key1 cannot be blank. ${propname} doesn't work. I can't use the value annotation as I want to save the value of the property in the message.
The best approach for this is to not depend on any Mule specific code and deal with the property as you would any other argument, just passing it as a parameter. So when instantiating or calling a certain method, you'd just pass in the property at the Mule side:
<java:new class="com.me.Person" constructor="Person(String, Integer)">
<java:args>#[{
name: Mule::p('prop1'),
age: 30
}]</java:args>
</java:new>
Otherwise you'll depend on how that Java code is loaded since you'll need to inject a ConfigurationProperties instance and use it to resolve your property:
#Inject
private ConfigurationProperties configurationProperties;
String getProperty(String name) {
return configurationProperties.resolveStringProperty(name).orElse(null);
}
This means your Java code has to be part of an SDK module so the injection takes place.
In our project (springMVC) Rest API project I wish to only use ONE model for both request and response (to avoid having to add tons of code to copy field from object to object)
I'd like to use Swagger to handle all the doc, but I'm running into a little problem. For example let say I have a model User
public class User {
private Long id;
private String username;
private String password;
}
And a simple controller
public void createUser(#RequestBody User user)...
public User getUser(Long id) ..
Now I would like swagger to hide the property password on deserialization but not serialization (so having it display for the Input but the output)
and the opposite for the Id field.
I have tried using #JsonIgnore coupled with #JsonProperty but on the swagager-ui it either displays everything or hides everything. I cannot manage to it work.
Could someone indicate me what is the best way of archiving my goal ? Is it possible to use a single model for request and response while using swagger? In case it is not possible to use #JsonIgnore, is there a way to archive this differently ?
Swagger doesn't want you to have different input/output models with the same name. You should simply create an interface and attach that to the input, and for the output extend that interface or add an implementation with the additional field. For example, please see here for modeling tips:
https://swaggerhub.com/api/swagger-tutorials/modeling-samples/1.0.0
Your exact use case is one of them. The solution posted in the above link is here:
definitions:
User:
description: this is a user that would be passed into the system
properties:
username:
type: string
UserResponse:
allOf:
- $ref: '#/definitions/User'
- type: object
required:
- id
properties:
id:
type: string
format: uuid
readOnly: true
where User is the input object, and UserResponse is the output object, with the additional id field.
Add #JsonIgnore with getter of the field and #JsonProperty with the setter or with the field . As Due to use of immutable code or final fields sometime setter doesn't work.
example :
public class Student {
private Float name;
private String rollnum;
private String section;
#JsonProperty
private Boolean passOrFailed;
#JsonIgnore
public Boolean getpassOrFailed {
return active;
}
}
Remember to use both else else it will lead to removing element in deserialization
I want to assign Group name as an attribute for authorize filter.
It will take as below
[FilterConfig.AuthorizeAd(Group = "DirectoryName")]
public ActionResult GetData()
{
}
Insted of hardcoding i tried by adding it as below
[FilterConfig.AuthorizeAd(Group = Constants.ActiveDirectoryName)]
Where Constants is class and created member as below:
public const string ActiveDirectoryName = "directoryName";
Now i want to aceess it from app.config, tried as below
[FilterConfig.AuthorizeAd(Group = ConfigurationManager.AppSettings["Directory_Name"].ToString()
It is throughing the error msg as "An attribute argument must be a constant expression"
How can i assign the data from config?
Please suggest me.
You can't do that with attributes, they have to be constants as stated in the error message. If you wanted to get a value from the configuration file, you could do it by passing the key to the attribute, and then in the constructor get the value you want from the configurationmanager
public MyAttribute :Attribute
{
private string _config;
public MyAttribute(string configKey)
{
_config = ConfigurationManager.AppSettings[configKey];
...
}
}
HTH