Skip to content

Commit

Permalink
Java enumeration and Spring Converter no longer generates enum drop-d…
Browse files Browse the repository at this point in the history
…own. Fixes #1992
  • Loading branch information
bnasslahsen committed Dec 12, 2022
1 parent d27acd9 commit 209c55b
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@

/**
* The type Generic parameter builder.
*
* @author bnasslahsen, coutin
*/
@SuppressWarnings("rawtypes")
Expand Down Expand Up @@ -355,8 +356,11 @@ Schema calculateSchema(Components components, ParameterInfo parameterInfo, Reque
Type type = ReturnTypeParser.getType(methodParameter);
if (type instanceof Class && optionalWebConversionServiceProvider.isPresent()) {
WebConversionServiceProvider webConversionServiceProvider = optionalWebConversionServiceProvider.get();
if (!MethodParameterPojoExtractor.isSwaggerPrimitiveType((Class) type) && methodParameter.getParameterType().getAnnotation(io.swagger.v3.oas.annotations.media.Schema.class) == null)
type = webConversionServiceProvider.getSpringConvertedType(methodParameter.getParameterType());
if (!MethodParameterPojoExtractor.isSwaggerPrimitiveType((Class) type) && methodParameter.getParameterType().getAnnotation(io.swagger.v3.oas.annotations.media.Schema.class) == null) {
Class<?> springConvertedType = webConversionServiceProvider.getSpringConvertedType(methodParameter.getParameterType());
if (!(String.class.equals(springConvertedType) && ((Class<?>) type).isEnum()))
type = springConvertedType;
}
}
schemaN = SpringDocAnnotationsUtils.extractSchema(components, type, jsonView, methodParameter.getParameterAnnotations());
}
Expand Down Expand Up @@ -569,6 +573,7 @@ public Optional<WebConversionServiceProvider> getOptionalWebConversionServicePro
/**
* Resolve the given annotation-specified value,
* potentially containing placeholders and expressions.
*
* @param value the value
* @return the object
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package test.org.springdoc.api.v30.app200;


import javax.annotation.Generated;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public enum FooBar {
FOO("foo"),
BAR("bar");

private String value;

FooBar(String value) {
this.value = value;
}

@JsonValue
public String getValue() {
return value;
}

@Override
public String toString() {
return String.valueOf(value);
}

@JsonCreator
public static FooBar fromValue(String value) {
for (FooBar b : FooBar.values()) {
if (b.value.equals(value)) {
return b;
}
}
throw new IllegalArgumentException("Unexpected value '" + value + "'");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package test.org.springdoc.api.v30.app200;


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

@RestController
public class FooBarController {
@GetMapping(value = "/example/{fooBar}")
public String getFooBar(@PathVariable FooBar fooBar) {
return fooBar.name();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package test.org.springdoc.api.v30.app200;


import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

@Component
public class FooBarConverter implements Converter<String, FooBar> {

@Override
public FooBar convert(String source) {
return FooBar.fromValue(source);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
*
* *
* * *
* * * * 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.app200;

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

import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringDocApp200Test extends AbstractSpringDocV30Test {

@SpringBootApplication
static class SpringDocTestApp {}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "http://localhost",
"description": "Generated server url"
}
],
"paths": {
"/example/{fooBar}": {
"get": {
"tags": [
"foo-bar-controller"
],
"operationId": "getFooBar",
"parameters": [
{
"name": "fooBar",
"in": "path",
"required": true,
"schema": {
"type": "string",
"enum": [
"foo",
"bar"
]
}
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
},
"components": {}
}

0 comments on commit 209c55b

Please sign in to comment.