Skip to content
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

remap 'allowableValues' and 'defaultValue' #148

Merged
merged 1 commit into from
Feb 18, 2020
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 @@ -741,11 +741,11 @@ protected Schema bindSchemaForElement(VisitorContext context, Element element, C
*/
protected Schema bindSchemaAnnotationValue(VisitorContext context, Element element, Schema schemaToBind, AnnotationValue<io.swagger.v3.oas.annotations.media.Schema> schemaAnn) {
JsonNode schemaJson = toJson(schemaAnn.getValues(), context);
return doBbindSchemaAnnotationValue(context, element, schemaToBind, schemaJson, schemaAnn.get("defaultValue", String.class).orElse(null),
return doBindSchemaAnnotationValue(context, element, schemaToBind, schemaJson, schemaAnn.get("defaultValue", String.class).orElse(null),
schemaAnn.get("allowableValues", String[].class).orElse(null));
}

private Schema doBbindSchemaAnnotationValue(VisitorContext context, Element element, Schema schemaToBind,
private Schema doBindSchemaAnnotationValue(VisitorContext context, Element element, Schema schemaToBind,
JsonNode schemaJson, String defaultValue, String[] allowableValues) {
try {
schemaToBind = jsonMapper.readerForUpdating(schemaToBind).readValue(schemaJson);
Expand All @@ -754,7 +754,9 @@ private Schema doBbindSchemaAnnotationValue(VisitorContext context, Element elem
}
if (ArrayUtils.isNotEmpty(allowableValues)) {
for (String allowableValue : allowableValues) {
schemaToBind.addEnumItemObject(allowableValue);
if (!schemaToBind.getEnum().contains(allowableValue)) {
schemaToBind.addEnumItemObject(allowableValue);
}
}
}
} catch (IOException e) {
Expand Down Expand Up @@ -796,7 +798,7 @@ protected Schema bindArraySchemaAnnotationValue(VisitorContext context, Element
}
}
}
return doBbindSchemaAnnotationValue(context, element, schemaToBind, schemaJson, null, null);
return doBindSchemaAnnotationValue(context, element, schemaToBind, schemaJson, null, null);
}

private Optional<Map<String, Object>> resolveExtensions(JsonNode jn) {
Expand Down Expand Up @@ -848,15 +850,25 @@ private void bindSchemaIfNeccessary(VisitorContext context, AnnotationValue<?> a
final Optional<String[]> anyOf = av.get("anyOf", Argument.of(String[].class));
final Optional<String[]> oneOf = av.get("oneOf", Argument.of(String[].class));
final Optional<String[]> allOf = av.get("allOf", Argument.of(String[].class));
if (io.swagger.v3.oas.annotations.media.Schema.class.getName().equals(av.getAnnotationName()) && impl.isPresent()) {
// remap keys.
Object o = valueMap.remove("defaultValue");
if (o != null) {
valueMap.put("default", o);
}
o = valueMap.remove("allowableValues");
if (o != null) {
valueMap.put("enum", o);
}
boolean isSchema = io.swagger.v3.oas.annotations.media.Schema.class.getName().equals(av.getAnnotationName());
if (isSchema && impl.isPresent()) {
final String className = impl.get();
bindSchemaForClassName(context, valueMap, className);
}
if (io.swagger.v3.oas.annotations.media.DiscriminatorMapping.class.getName().equals(av.getAnnotationName()) && schema.isPresent()) {
final String className = schema.get();
bindSchemaForClassName(context, valueMap, className);
}
if (io.swagger.v3.oas.annotations.media.Schema.class.getName().equals(av.getAnnotationName()) && (anyOf.isPresent() || oneOf.isPresent() || allOf.isPresent())) {
if (isSchema && (anyOf.isPresent() || oneOf.isPresent() || allOf.isPresent())) {
anyOf.ifPresent(anyOfList -> bindSchemaForComposite(context, valueMap, anyOfList, "anyOf"));
oneOf.ifPresent(oneOfList -> bindSchemaForComposite(context, valueMap, oneOfList, "oneOf"));
allOf.ifPresent(allOfList -> bindSchemaForComposite(context, valueMap, allOfList, "allOf"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2017-2019 original 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
*
* http://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 io.micronaut.openapi.visitor

import io.micronaut.annotation.processing.test.AbstractTypeElementSpec
import io.micronaut.http.MediaType
import io.swagger.v3.oas.annotations.Parameter
import io.swagger.v3.oas.annotations.enums.ParameterIn
import io.swagger.v3.oas.annotations.media.ArraySchema
import io.swagger.v3.oas.annotations.media.Content
import io.swagger.v3.oas.annotations.media.Schema
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.Operation

class OpenApiOperationParametersSpec extends AbstractTypeElementSpec {
def setup() {
System.setProperty(AbstractOpenApiVisitor.ATTR_TEST_MODE, "true")
}

void "test Parameters in Operation"() {
given:
buildBeanDefinition('test.MyBean', '''
package test;

import io.swagger.v3.oas.annotations.*;
import io.swagger.v3.oas.annotations.parameters.*;
import io.swagger.v3.oas.annotations.responses.*;
import io.swagger.v3.oas.annotations.security.*;
import io.swagger.v3.oas.annotations.media.*;
import io.swagger.v3.oas.annotations.enums.*;
import io.swagger.v3.oas.annotations.links.*;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.*;
import java.util.List;

@Controller("/")
class MyController {

@Get("/")
@Operation(description = "Lists the Pets.",
parameters = {
@Parameter(name = "petType", in = ParameterIn.HEADER, required = true, description = "A pet type", example = "['dog', 'cat']", schema = @Schema(description = "A _Pet_'s type", type = "string", allowableValues = {"dog", "cat", "snake"}, defaultValue = "dog"))
}
)
@ApiResponse(responseCode = "200", description = "Returns a _Pet_.", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = Pet.class)))
public Pet findPets(String petType) {
return null;
}
}

@Schema(description = "Pet")
class Pet {
@Schema(description = "The name of the pet")
public String name;
}

@javax.inject.Singleton
class MyBean {}
''')

OpenAPI openAPI = AbstractOpenApiVisitor.testReference
Operation operation = openAPI.paths?.get("/")?.get

expect:
operation
operation.responses.size() == 1
operation.responses.'200'.description == 'Returns a _Pet_.'
operation.responses.'200'.content.'application/json'.schema.$ref == '#/components/schemas/Pet'

operation.parameters
operation.parameters.size() == 1
operation.parameters[0].name == 'petType'
operation.parameters[0].description == 'A pet type'
operation.parameters[0].schema.description == 'A _Pet_\'s type'
operation.parameters[0].schema.type == 'string'
operation.parameters[0].schema.enum
operation.parameters[0].schema.enum == ["dog", "cat", "snake"]
operation.parameters[0].schema.default == 'dog'
}
}