Skip to content

Commit

Permalink
@JsonUnwrapped is ignored when PolymorphicConverter is enabled. Fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
bnasslahsen committed Sep 21, 2024
1 parent 1fc8f9d commit 7bb5fc2
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@

package org.springdoc.core.converters;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.databind.JavaType;
import io.swagger.v3.core.converter.AnnotatedType;
import io.swagger.v3.core.converter.ModelConverter;
Expand All @@ -39,6 +41,7 @@
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.springdoc.core.providers.ObjectMapperProvider;

/**
Expand Down Expand Up @@ -98,6 +101,11 @@ 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)) {
PARENT_TYPES_TO_IGNORE.add(javaType.getRawClass().getSimpleName());
}
}
if (chain.hasNext()) {
if (!type.isResolveAsRef() && type.getParent() != null
&& PARENT_TYPES_TO_IGNORE.stream().noneMatch(ignore -> type.getParent().getName().startsWith(ignore)))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package test.org.springdoc.api.v30.app224;

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,35 @@
/*
*
* *
* * *
* * * *
* * * * * Copyright 2019-2022 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.app224;

import test.org.springdoc.api.v30.AbstractSpringDocV30Test;

import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringDocApp224Test 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.app224;

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.app224;

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"
}
}
}
}
}
}

0 comments on commit 7bb5fc2

Please sign in to comment.