Skip to content

fix: respect @JsonUnwrapped & @Schema on props not fields only #2894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import io.swagger.v3.core.converter.AnnotatedType;
import io.swagger.v3.core.converter.ModelConverter;
import io.swagger.v3.core.converter.ModelConverterContext;
Expand Down Expand Up @@ -120,17 +122,28 @@ else if (resolvedSchema.getProperties().containsKey(javaType.getRawClass().getSi
public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) {
JavaType javaType = springDocObjectMapper.jsonMapper().constructType(type.getType());
if (javaType != null) {
for (Field field : FieldUtils.getAllFields(javaType.getRawClass())) {
if (field.isAnnotationPresent(JsonUnwrapped.class)) {
BeanDescription javaTypeIntrospection = springDocObjectMapper.jsonMapper().getDeserializationConfig().introspect(javaType);
for (BeanPropertyDefinition property : javaTypeIntrospection.findProperties()) {
boolean isUnwrapped = (property.getField() != null && property.getField().hasAnnotation(JsonUnwrapped.class)) ||
(property.getGetter() != null && property.getGetter().hasAnnotation(JsonUnwrapped.class));

if (isUnwrapped) {
if (!TypeNameResolver.std.getUseFqn())
PARENT_TYPES_TO_IGNORE.add(javaType.getRawClass().getSimpleName());
else
PARENT_TYPES_TO_IGNORE.add(javaType.getRawClass().getName());
}
else if (field.isAnnotationPresent(io.swagger.v3.oas.annotations.media.Schema.class)) {
io.swagger.v3.oas.annotations.media.Schema declaredSchema = field.getDeclaredAnnotation(io.swagger.v3.oas.annotations.media.Schema.class);
if (ArrayUtils.isNotEmpty(declaredSchema.oneOf()) || ArrayUtils.isNotEmpty(declaredSchema.allOf())) {
TYPES_TO_SKIP.add(field.getType().getSimpleName());
else {
io.swagger.v3.oas.annotations.media.Schema declaredSchema = null;
if (property.getField() != null) {
declaredSchema = property.getField().getAnnotation(io.swagger.v3.oas.annotations.media.Schema.class);
} else if (property.getGetter() != null) {
declaredSchema = property.getGetter().getAnnotation(io.swagger.v3.oas.annotations.media.Schema.class);
}

if (declaredSchema != null &&
(ArrayUtils.isNotEmpty(declaredSchema.oneOf()) || ArrayUtils.isNotEmpty(declaredSchema.allOf()))) {
TYPES_TO_SKIP.add(property.getPrimaryType().getRawClass().getSimpleName());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package test.org.springdoc.api.v30.app239;

import com.fasterxml.jackson.annotation.JsonUnwrapped;

public class RootModel {

private Integer rootProperty;

private UnwrappedModel unwrappedModel;

public Integer getRootProperty() {
return rootProperty;
}

public void setRootProperty(Integer rootProperty) {
this.rootProperty = rootProperty;
}

@JsonUnwrapped
public UnwrappedModel getUnwrappedModel() {
return unwrappedModel;
}

public void setUnwrappedModel(UnwrappedModel unwrappedModel) {
this.unwrappedModel = unwrappedModel;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
*
* *
* * *
* * * *
* * * * * Copyright 2019-2024 the original author or authors.
* * * * *
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
* * * * * you may not use this file except in compliance with the License.
* * * * * You may obtain a copy of the License at
* * * * *
* * * * * https://www.apache.org/licenses/LICENSE-2.0
* * * * *
* * * * * Unless required by applicable law or agreed to in writing, software
* * * * * distributed under the License is distributed on an "AS IS" BASIS,
* * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * * * * See the License for the specific language governing permissions and
* * * * * limitations under the License.
* * * *
* * *
* *
*
*/

package test.org.springdoc.api.v30.app239;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;

public class SpringDocApp239Test extends AbstractSpringDocV30Test {

@SpringBootApplication
static class SpringDocTestApp {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package test.org.springdoc.api.v30.app239;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class TestController {

@GetMapping
public RootModel getRootModel() {
return new RootModel();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package test.org.springdoc.api.v30.app239;

public class UnwrappedModel {

private Integer unwrappedProperty;

public Integer getUnwrappedProperty() {
return unwrappedProperty;
}

public void setUnwrappedProperty(Integer unwrappedProperty) {
this.unwrappedProperty = unwrappedProperty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ public class RootModel {

private Integer rootProperty;

@JsonUnwrapped
private UnwrappedModel unwrappedModel;

public Integer getRootProperty() {
Expand All @@ -17,6 +16,7 @@ public void setRootProperty(Integer rootProperty) {
this.rootProperty = rootProperty;
}

@JsonUnwrapped
public UnwrappedModel getUnwrappedModel() {
return unwrappedModel;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package test.org.springdoc.api.v31.app239;

import com.fasterxml.jackson.annotation.JsonUnwrapped;

public class RootModel {

private Integer rootProperty;

@JsonUnwrapped
private UnwrappedModel unwrappedModel;

public Integer getRootProperty() {
return rootProperty;
}

public void setRootProperty(Integer rootProperty) {
this.rootProperty = rootProperty;
}

public UnwrappedModel getUnwrappedModel() {
return unwrappedModel;
}

public void setUnwrappedModel(UnwrappedModel unwrappedModel) {
this.unwrappedModel = unwrappedModel;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
*
* *
* * *
* * * *
* * * * * Copyright 2019-2024 the original author or authors.
* * * * *
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
* * * * * you may not use this file except in compliance with the License.
* * * * * You may obtain a copy of the License at
* * * * *
* * * * * https://www.apache.org/licenses/LICENSE-2.0
* * * * *
* * * * * Unless required by applicable law or agreed to in writing, software
* * * * * distributed under the License is distributed on an "AS IS" BASIS,
* * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * * * * See the License for the specific language governing permissions and
* * * * * limitations under the License.
* * * *
* * *
* *
*
*/

package test.org.springdoc.api.v31.app239;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import test.org.springdoc.api.v31.AbstractSpringDocTest;

public class SpringDocApp239Test extends AbstractSpringDocTest {

@SpringBootApplication
static class SpringDocTestApp {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package test.org.springdoc.api.v31.app239;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class TestController {

@GetMapping
public RootModel getRootModel() {
return new RootModel();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package test.org.springdoc.api.v31.app239;

public class UnwrappedModel {

private Integer unwrappedProperty;

public Integer getUnwrappedProperty() {
return unwrappedProperty;
}

public void setUnwrappedProperty(Integer unwrappedProperty) {
this.unwrappedProperty = unwrappedProperty;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "http://localhost",
"description": "Generated server url"
}
],
"paths": {
"/api": {
"get": {
"tags": [
"test-controller"
],
"operationId": "getRootModel",
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/RootModel"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"RootModel": {
"type": "object",
"properties": {
"rootProperty": {
"type": "integer",
"format": "int32"
},
"unwrappedProperty": {
"type": "integer",
"format": "int32"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"openapi": "3.1.0",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "http://localhost",
"description": "Generated server url"
}
],
"paths": {
"/api": {
"get": {
"tags": [
"test-controller"
],
"operationId": "getRootModel",
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/RootModel"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"RootModel": {
"type": "object",
"properties": {
"rootProperty": {
"type": "integer",
"format": "int32"
},
"unwrappedProperty": {
"type": "integer",
"format": "int32"
}
}
}
}
}
}