Skip to content

Commit

Permalink
refs swagger-api#4662 - If a property has no annotation, but only a g…
Browse files Browse the repository at this point in the history
…etter, it should be read-only.
  • Loading branch information
beyond-seunghyun committed Apr 25, 2024
1 parent 616350b commit e89a83b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1975,6 +1975,9 @@ protected io.swagger.v3.oas.annotations.media.Schema.AccessMode resolveAccessMod
if (!hasGetter && !hasField && (hasConstructorParameter || hasSetter)) {
return io.swagger.v3.oas.annotations.media.Schema.AccessMode.WRITE_ONLY;
}
if (hasGetter && !hasField && !hasConstructorParameter && !hasSetter) {
return io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_ONLY;
}
return null;
} else {
switch (access) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.swagger.v3.core.oas.models;

public class Cap {

public String getBrand() {
return "no brand";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.core.converter.ModelConverters;
import io.swagger.v3.core.matchers.SerializationMatchers;
import io.swagger.v3.core.oas.models.Cap;
import io.swagger.v3.core.oas.models.Car;
import io.swagger.v3.core.oas.models.Manufacturers;
import io.swagger.v3.core.oas.models.ReadOnlyModel;
Expand Down Expand Up @@ -150,6 +151,24 @@ public void makeFieldReadOnly() throws IOException {
SerializationMatchers.assertEqualsToJson(schemas, json);
}

@Test(description = "it should make a field readOnly, if there only getter")
public void makeGetterReadOnly() throws IOException {
final Map<String, Schema> schemas = ModelConverters.getInstance().read(Cap.class);
final String json =
"{\n" +
" \"Cap\":{\n" +
" \"type\":\"object\",\n" +
" \"properties\":{\n" +
" \"brand\":{\n" +
" \"type\":\"string\",\n" +
" \"readOnly\":true\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
SerializationMatchers.assertEqualsToJson(schemas, json);
}

@Test(description = "it should serialize a model with a Set")
public void serializeModelWithSet() throws IOException {
final Map<String, Schema> schemas = ModelConverters.getInstance().read(Manufacturers.class);
Expand Down

0 comments on commit e89a83b

Please sign in to comment.