Returning null values ​​from a web service call

I have a web services API. Some calls return objects containing text fields with information provided by the user. Both in terms of design and in terms of security, what are the flaws that return zero in these fields when no information has been provided? Is there a clear advantage that you always return an empty string and then simplify the API without requiring the client code to check for zeros?

+4
source share
6 answers

It all depends on whether you treat the value of the null string as semantically different from the empty string.

If a null and empty string means that there is no data for this field, I see no reason not to make life easier for the client without checking and returning an empty string.

+1
source

Short answer: Do not use null in web services.

In general, I advise you to stick to empty lines above zero, unless the value is non-zero. I would prefer to use null only as "undefined". For example, when entering a user, if the user enters the field and does not enter anything, this will be an empty string. But if the user simply skips this field, it may be null.

Until I define a value for null, I prefer to return an empty string and use String.IsNUllOrEmpty on the processing side, because instead of any future knowledge, I should assume that the empty and empty are the same.

- , , <element/>, <element></element> , . , , , , .

, , , , , /.

+1

. , -, , null .

, , -, Adobe Flex, , , .

.

+1

, null .

null , - , .

,

string.IsNullOrEmpty()

( , .NET)

0

Null null. .

- API, . , null ( ).

, , . , .

, xml, .net SoapFormatter null, , , . , . , , - .

{
    [WebMethod]
    public MyClass HelloWorld() 
    {
        MyClass val = new MyClass()
        {
            IsValid = false,
            HelloString = "Hello World",
            BlankString = "",
            Nested = new NestedClass { Name = "Bob" }
        };

        return val;
    }

}

public class MyClass
{
    public bool IsValid { get; set; }
    public string HelloString { get; set; }
    public string BlankString { get; set; }
    public string OtherString { get; set; }
    public NestedClass Nested { get; set; }
    public NestedClass NullNested { get; set; }
}

public class NestedClass
{
    public string Name { get; set; }
}

xml-. , OtherString NullNested , BlankString.

<MyClass>
  <IsValid>false</IsValid> 
  <HelloString>Hello World</HelloString> 
  <BlankString /> 
  <Nested>
    <Name>Bob</Name> 
  </Nested>
</MyClass>
0

just for completeness, assuming you are returning a row or a set of data rows as a JSON string, there is always the option to completely skip fields with zero values. this results in less data being transmitted over the network, and can usually be easily processed by any client (obviously, such an agreement should be documented somewhere). see, for example, the following Jackson configuration (it also skips empty collections):

private static ObjectMapper configureMapper(ObjectMapper mapper) {
  mapper.setDefaultPropertyInclusion(JsonInclude.Value.construct(JsonInclude.Include.NON_EMPY, JsonInclude.Include.NON_NULL));
  mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
  return mapper;
}
0
source

Source: https://habr.com/ru/post/1697695/


All Articles